What exactly is a parameter?

Say for example, we had just a simple variable

local myVariable = "epic"

if we were to turn this into a function with a parameter, myVariable would be the parameter and "epic" would be the argument (this is how it would look as a function)

function cool(myVariable)
    print(myVariable)
end

cool("epic")

By the way, cool would be the name of the function, so whenever you wanted to run whatever was in there, you would call it by doing cool("whatever you wanna print here")

2 Likes

When calling a function normally though without any parameters, you just have the parenthesis without anything inside of them, like this: cool()

2 Likes

function cool()
print()
end

cool()

what would happen if the script looked like this?

1 Like

It’d print out nothing, though if you had something like

function cool(message)
    print(message)
end

cool("The console will print out this message.")

it’d print out The console will print out this message.

or you could do something like

function cool()
    print("The console will print out this message")
end

cool()

and it’d do the same thing, except you can’t modify what it says

1 Like

something that makes parameters super useful, is instead of having multiple functions like

function cool1()
    print("This was printed from the function cool1")
end
function cool2()
    print("This was printed from the function cool2")
end
function cool3()
    print("This was printed from the function cool3")
end

cool1()
cool2()
cool3()

you can just do something like

function cool(message)
    print(message)
end

cool("This was printed from the function cool")
cool("This was also printed from the function cool")
cool("This message as well")
cool("And this")
cool("As well as this")

so even though your function only has 1 print(), you can print whatever you want as many times as you want

2 Likes

Uhm… sorry if this is a really stupid question, but what does “print” do?

1 Like

So basically, your game has an output, where it tells you if your script errored, or if you printed anything. print() basically just writes whatever is inside of the parenthesis into the output, very useful for debugging your scripts and checking which parts of your script isn’t working.

If you wanted to print a string, it’d look like this:

print("This is a string")

Keep in mind that when printing strings, you must have quotation marks.

You can also print out variables (which are basically what parameters are except they’re inside functions)

To print out a variable, you do print() and put the name of that variable inside of the parenthesis, for example:

local message = "Hello world!"

print(message) --this prints out: Hello world!

print("message") -- this prints out: message
2 Likes

Parameter is the things inside () after a function call

Imagine a machine that performs a specific task: Cook food
Obviously when I say “food”, you can cook anything as long as it is food.

Cook an apple you get a cooked apple
Cook a chicken you get a cooked chicken

apple and chicken are parameters you feed into the machine that cooks food

cooked apple and cooked chicken are the things that the machine process our food into.

In code, it looks like this:
(For the sake of simplicity, the machine processes the name of the food instead of actual food )

function CookFood(foodName)
   local cookedFood = "Cooked"..foodName-- Let's say there's some code to increase temperature of the machine
   wait(10) -- Wait in seconds for the food to be cooked
   return cookedFood  
end

That’s basically what parameters are for, they change the output of a function based on input

Other examples are functions that damage a character, where you put in the damage amount and the character to damage as parameter.

there’s “return” in there, it tells the function to stop and
(any code below return in the same scope (the function in this case) will not run)

basically become the value it returns, like

local food = CookFood("Apple")

If you print

print("Today I cooked some "..food)

It will output
Today I cooked some Cooked Apple

The “..” thingy tells the script to combine the string with the thing beside it, usually another string

3 Likes

All it is:
(Sorry about the format, on mobile).

local function CoinAdd(Coins)
game.Players.leaderstats += Coins
end

CoinAdd(30) — Will add 30 coins.

1 Like

Here’s something which might be useful!
Operators!

1 Like