Simple Roblox Lua Function
Here is a simple Roblox Lua function with explanations for each step:
-- Function to double a number
function doubleNumber(number)
-- Multiply the number by 2
local result = number * 2
-- Return the doubled number
return result
end
-- Call the function with an example number
local exampleNumber = 5
local doubledNumber = doubleNumber(exampleNumber)
-- Print the result
print("The doubled number is: " .. doubledNumber)
Explanation:
- The code starts by defining a function named
doubleNumber
, which takes in a parameternumber
. - Inside the function, the
number
parameter is multiplied by 2 and stored in a local variable namedresult
. - The function then returns the value of
result
. - After the function definition, the code continues with the main program logic.
- A variable named
exampleNumber
is declared and assigned the value of 5. - The
doubleNumber
function is called withexampleNumber
as an argument, and the returned value is stored in a variable nameddoubledNumber
. - Finally, the program prints the result by concatenating the string "The doubled number is: " with the value of
doubledNumber
.