Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

return help? [closed]

Asked by 8 years ago

This question already has an answer here:

How to use return effectively?

how do i use return properly besides printing and booleans?

much appreciated, thanks

0
I had a question same as this a while back. Here's the link, https://scriptinghelpers.org/questions/28462/how-does-return-work-and-what-is-it-used-for User#11440 120 — 8y

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?

2 answers

Log in to vote
1
Answered by 8 years ago

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.

Ad
Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

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.


Hopefully this answered your question. If it did, do not forget to hit the accept answer button to the right. If you have any questions, feel free to leave them in the comments down below.
1
youm got problem and you are not helpful Lem0nzzx 70 — 8y
1
That doesn't really answer his question dude, come on. -_- Avectus 120 — 8y