Note: This answer assumes (kinda) that you have knowledge on for loops, functions, tuples, etc. If not, links/explanations are supplied at the end of the answer.
Return is a function that, well, returns a value (or values) when prompted to. In the case of your script, it returns an index (I like to call it an iteration/iterator, as you'll see later on). The return function can be used for different purposes, such as returning a value in a table:
local exampletable = {'Hello', 'World'}
function GetValue(tbl, val)
for i, v in next, tbl do
if v == val then
return i, v
end
end
return nil, nil
end
local i, v = GetValue(exampletable, 'World')
print(i, v)
As you'll see (or noticed XP) it'll return what the current iteration in the table was, and what value it was on when it found the match. To note, you don't always need to use return like that; you can even use it for CONFUSING MATH!!! >>:O
function Add(...)
local numbahs = {...}
local num = 0
for i, v in next, numbahs do
num = num + v
end
return num
end
print(Add(5, 7, 1, 3), 'was the final result')
And as you'll see (again XP) it returned all the numbers added up! :D
I'm tired, so I'm gonna leave here. Bai...
GET BACK HERE! FINISH IT!
Alright. ;-;
There's so many possibilities you can use return for, and there's many advantages to using it! Don't be afraid to use it, or anything else you may discover! You might be surprised in what you might find. :)
NOW, ONTO THE ST00F!!!! >>:OOOOOOOOO
St00f touched on, yet barely explained by this fr00b
Tuples - The ability to use as many arguments in a function as much as you want! :D The catch: You gotta account for each and every one, and (don't quote me on this) put it in a table. qq
Generic Fors - A for loop that returns both the iteration and value in a table. Only catch is that you need to give it a table. (Please don't quote me on that qq)
Return - Returns values given to it.
Tyvm for reading! ^^ I hope this helped!
3 comments
:GetPlayers() returns an array, the first value
_is the index, and therefore a numberOh wow, seems pretty obvious now that you say it haha. Thank you. It just made me question it because it had a blue error line under the '_'
To add, using underscore variables is bad practice (thats why studio warns you) so who cares. As long as you understand it's the same as putting "i,v" ; using i.