Is it possible to make a function immediately stop, or become nil or something?
When a function return
s a value, it exits and produces that value as result.
A function may also return
nothing (which will be nil
), which will still exit the funciton.
Loops (for
, while
, repeat
) have a similar functionality in break
which just exits the current loop. return
exits the current function, unirregardlessly of how many loops / blocks the statement is in:
function running() i = 0 while true do i = i + 1 if math.random() < 0.0001 then return i end end end print( running() ) -- a large number will be printed
I'm not sure about functions, but you can stop loops with break
.
For example, if you wanted a for
loop to end if a condition is met, you could use this code:
for i,v in pairs(game.Players:GetChildren()) do if game.Workspace.Condition.Value == true then break end end
Read more on the Wiki.
functionname:disconnect()