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

Stopping a function mid-way?

I have a function which makes plates dissapear, but once the Model is empty it will give an error, as there are no more plates left, and break the whole script, here's what I have:

local Grass = game.Workspace.Classic.Grass:GetChildren()
if #Grass == 0 then
-- I'm not sure how to "stop" the rest of the function
else
-- Plates code

1 answer

✓ Accepted answer

If you want to exit the code, simply use 'return'. You can learn more about return here: http://wiki.roblox.com/index.php?title=Function#Using_Return

This is the code you should use.

local Grass = game.Workspace.Classic.Grass:GetChildren()
if #Grass == 0 then
	return -- Exits this if #Grass is 0
else
	-- Plates code
end

There's another way you could do this to remove a few lines by just checking if grass is not 0.

local Grass = game.Workspace.Classic.Grass:GetChildren()
if #Grass ~= 0 then -- If #Grass isn't equal 0 then continue
	-- Plates code
end

1 comment

0

Appreciated :)

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