Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Stopping a function mid-way?

Asked by
iFlusters 355 Moderation Voter
9 years ago

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

Log in to vote
4
Answered by 9 years ago

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
0
Appreciated :) iFlusters 355 — 9y
Ad

Answer this question