how do i use return properly besides printing and booleans?
much appreciated, thanks
return Is very useful when you want to either block anything out in a function, or when you want to return the function to the beginning of when it was first fired.
The wiki mentions a lot of other good things return can do. If you need any more help, you can message me, and I can help you out! Here's an example of blocking out code in a function.
script.Parent.Touched:connect(function() return print("Test") end)
Instead of it printing test it will return back to the beginning.
To have returns function properly you would need to do so in a function. What a return is is basically changing the function you're calling into a value or just grabbing the result from it.
Say we were to make our own math function, we'll just do addition since that is easier.
function Add(x, y) return x + y end print(Add(5, 6))
Think of return
putting the sum of 5 and 6 in place of the Add(5, 6)
. The function we have is taking the two arguments when the function is called, adding them together, and returning the result of that value. So in this case the output would return 11.
Say you were making an admin script and we have a very basic list of Admins.
Admins = {'OnlyTwentyCharacters'} function IsAdmin(Player) for i,v in pairs(Admins) do if Player.Name:lower() == v:lower() then return true end end return false end game.Players.PlayerAdded:connect(function(Player) if IsAdmin(Player) then --The function will return true or false, the function must return true to pass this part of the if then statement. print('The player is an admin!') else print('Nope, just a regular player...') end end)
Return will instantly exit the function it is in to return the result as it does when the Player's name is a match to someone in the list. If we were to make it to the end of the admin list, we would want to return false so that we don't get a possible error. I have not tested it but I believe the result would be nil and false anyway if you didn't have return false
but for simplicity sake we added it.
Marked as Duplicate by BlueTaslem
This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.
Why was this question closed?