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

How does return work and what is it used for?

Asked by 9 years ago

So I've only used return for on thing and that's to return end. Ex,

1if not player then return end

But I really don't know anything else. Let's say I do something like this,

1local x = 5
2function oddOrEven(x)
3   if x%2 == 0 then
4      return false
5   else
6      return true
7   end
8end
9oddOrEven(x)

How would you get if its true or false. And yes this might be unnecessary in the example but because of my lack of knowledge on returns I don't really know what they would be used for. If someone could tell me how return works and what it's used for I would be really happy (:

Also, I'm okay at roblox scripting I just haven't gotten around to how return works.

2 answers

Log in to vote
5
Answered by
legosweat 334 Moderation Voter
9 years ago

When using return:

When return is used in a function, the function stops executing. The function now holds the values returned. * Notice that *return does not execute in a Module Script.

Examine this code to understand this idea better: Holding a String Value:

01function GetRandomString()
02    local texts = {"Hello, I'm R15E", "Welcome to Whatever", "Hello World"} -- Some random text
03    local randomize = math.random(1,#texts) -- Get a random text
04    local picked = texts[randomize] -- Get the string
05    return picked -- Code would not work after this is stated, if there is, it would error.
06end
07 
08function PrintRandom()
09    local RandomText = GetRandomString() -- The string value would be returned to this value.
10    print(RandomText) -- Now we print the value.
11end
12 
13PrintRandom()

Another Example Holding a Boolean Value:

01local Enabled = true
02 
03function Check() -- Checks if the value is true
04    if Enabled then
05        return true -- this obviously returns "true"
06    else
07        return false -- this obviously returns "false"
08    end
09end
10 
11if Check() then -- In this case, it returns true, the rest of the code is executed.
12    game.Workspace.Music:Play() -- Just something random happens if it returns true.
13end

Obviously the above code wouldn't need to execute the code as you can just check if the value is true or false. But, it's just an example.

0
Awesome explanation! Thanks! User#11440 120 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

return puts whatever is returned where the function is called. For example:

1local function addNum(x, y)
2    return x+y;
3end
4 
5print(addNum(2, 2)); --in this case prints 4
6 
7-- putting return in the function makes the function call act like this:
8print(2+2);

You could also use return for logic comparisons.

01local function minPlayersReached()
02    if game.Players.NumPlayers >= 2 then
03        return true;
04    else
05        return false;
06    end
07end
08 
09if gameStarted and minPlayersReached() then --assuming gameStarted is some defined variable
10    callNextFunction();
11end

Additionally, return exits a function.

1
This is useless, I can't really understand why is it used for. Please don't do math cause that's what the wiki is for. Do an example that uses what the asker wants GeezuzFusion 200 — 9y
1
poorly explained. rexbit 707 — 9y
1
The math isn't the point, it's just for example purposes. Plus, it's not easy to explain in words. Do you want to try to do better? GoldenPhysics 474 — 9y
0
I don't know why people hated this post so much :/ User#11440 120 — 8y

Answer this question