Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Stopping Functions from continuing?

Is it possible to make a function immediately stop, or become nil or something?

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()

2 comments

0

Functions don't have a disconnect method. Even if they did, this couldn't stop current execution.

0

This is only for Event connections, not functions.

This discussion is preserved as it was. New answers, comments, and votes are closed.