Can you guys help me understand this?

So in this game loop, there is a timer that loops and soon I get to a part that confuses me with returning

‘’
– Stops the clock if gameover
if status == “Game” and gameOver then – if we are done with the game then we want to reset the timer
roundInfo.Timer.Value = 0
return – this is the part I am confused on
end
end
wait(1)
end
‘’

why are they returning nothing, or what are they doing because why are they just putting “return”

I know this is a total beginner question, but can you guys help me?

return always stops the function that’s running regardless if you return anything. They are trying to stop the function.

1 Like

because it is just a

return

if you wear this

return 'the imposter kinda sus..'

will return the string

ohh, so its like a stop block for a function?

Sort of. Usually when a function has a return in it, it’s usually called in a variable like:
local result = functionName()

Whatever the function returns will be stored in that result variable. An empty return is gonna be nil whilst anything you put after the return, will ofc be stored in that result variable. Like a string or something.

so then why are they even returning?

Depends, for example you wanna have cleaner code. A script can hold multiple functions. Instead of putting everything in one, we split them respectfully. So for example I have a function that loads stats on player join. I could put all the logic in one function but why would I? I’ll simply make a separate function to call a dataStore and have the function return the variable holding that player specific data.

ohhh so like for example

function addNumbers(a,b)
return a + b

end

Exactly. Whenever u need to use the function, you can call it and have it return the result instead of writing it over and over

ohh ok! that makes sense :slight_smile:

1 Like

Now, if you call local c = addNumbers(5,7) print(c)
You would get 12.

aaa ok! :slight_smile:

wow thank you guys, I gotta go but I’ll come back to here soon!

thanks :smiley:

1 Like

Putting return in a function will basically cancel the function, and return the value you put after it. For example, if you set a function to equal a variable (done like this):

local thingReturns = MyFunction()

then the variable will equal whatever value MyFunction() returns. If MyFunction is

function MyFunction()
    return "cool function"
end

then the variable thingReturns will be equal to “cool function”.
It might help you out if you messed around with it for a little bit, I will give you a few lines of code to help you better understand it.

Practice Code:

local function MyFunction()
    return "Change this to pretty much anything you want!" -- When you use return, you can return more types of values other than strings! It can return numbers, booleans, tables, and even objects!
end

local myVariable = MyFunction()
print(myVariable)

Hope this helps!

2 Likes

oh! ok thank you so much! This is great!

But just to make sure I understand this,

like lets say we want to add a hit box to a player and we have it and we want it to be sent to another function

local Function TheFunctionWithTheHitBox()

– the hitbox code here

return Hitbox
end

local FunctionThatIsGonnaPickUpTheReturn()

local hitbox = TheFunctionWithTheHitBox()

end

so I think I am giving the first function the hitbox and I returned the hit box and made a variable in the other function to pick it up.

is this right?

Yes. Depending upon the rest of the code, it should work.

Make sure that, when someone solves your problem, you mark the message that helped you as “Solved.”
This way other people who go on to this forum know what post they should look at to solve their problem, and also other people will know that this problem was already fixed. Thanks!

1 Like