Is it possible to make a function immediately stop, or become nil or something?
COMMUNITY QUESTION
Stopping Functions from continuing?
3 answers
When a function returns 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()
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Functions don't have a
disconnectmethod. Even if they did, this couldn't stop current execution.This is only for Event connections, not functions.