Using Variables Correctly: Storing and Reusing Values

Variables are like small boxes where you can store information, such as numbers or text. They help you keep your code organized and reuse values multiple times.

Here's how:

  1. Create a variable: Write the name of the variable and assign the value you want to store using =.
  2. Store a value: You can store numbers, texts (strings), or boolean values (true/false).
  3. Use the variable: Use the variable name later in the code to access or change the value.
  4. Change variables: You can reassign a variable at any time to update the stored value.
  5. Choose variable names: Names should be short but meaningful, e.g., "score" for points or "playerName" for the player's name.

With variables, you can make your Luau code much more flexible and easier to understand – just give it a try!

-- Dieses Script kommt z.B. in ServerScriptService
local score = 0 -- Punkte speichern
local playerName = "Spieler123" -- Name speichern

-- Funktion, um Punkte zu erhöhen
local function addPoints(points)
    score = score + points -- Punkte addieren
    print(playerName .. " hat jetzt " .. score .. " Punkte.")
end

-- Beispiel-Aufruf der Funktion
addPoints(10)