I've seen script use the term "return" before, but I don't get it even after looking in the official lua page. Can you please explain me how it works, exactly?
P.S If return isn't a function just ignore it, I need it to post this xd
So yeah. I had a lot of trouble figuring out what return does. So I hope I can help you out with this explanation: First of all return allows you to use a function like a variable here is an example:
1 | function randomNumber() |
2 | return math.random( 1 , 10 ) |
3 | end |
4 | local number = randomNumber() |
5 | print (number) |
6 | -- would output a random number between 1 and 10 |
7 | -- you could also do this: |
8 | print (randomNumber()) |
9 | -- would output a random number between 1 and 10 |
another benefit of return is it ends the function when it is called. Like in this example admin script:
01 | local adminList = { "Plieax" , "Bob" , "Joe" } -- names are examples |
02 |
03 | function checkIfPlayerIsAdmin(player) -- will equal false if the player is not an admin like a variable as I said above |
04 | for i,v in pairs (adminList) do |
05 | if v = = player.Name then |
06 | return true -- will end the loop right away and the function will be a variable which = true |
07 | end |
08 | end |
09 | return false -- if the function is not ended by the return true above in the for loop then the function will be a variable that = false |
10 | end |
11 | game.Players.PlayerAdded:connect( function (player) |
12 | if checkIfPlayerIsAdmin(player) then |
13 | print ( "PlayerIsAdmin" ) |
14 | end |
15 | end ) |
You can return any type of value a number, boolvalue, string, table, function, variable and so on. If you have anymore questions just comment below. I hope this helped. Have a great day scripting!
Not sure what you mean with 'P.S If return isn't a function just ignore it, I need it to post this xd" . Return are used for a few things, you probably most of the time see it inside functions
A quick example
1 | function MultiplyBy 2 (Val) |
2 | if Val then |
3 | return Val * 2 |
4 | end |
5 | end |
6 |
7 | local Val = MultiplyBy 2 ( 2 ) |
8 | print (Val) --> 4 |
It basicly creates a return value, you can have multiple return values too, or you can return a table
Also, modules can have return values too, that's the reason you should use modules. You can return a value the same way as the function above