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?
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.
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.
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)
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!