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
10 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:

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

1 answer

Log in to vote
4
Answered by 10 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.

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

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

1local Grass = game.Workspace.Classic.Grass:GetChildren()
2if #Grass ~= 0 then -- If #Grass isn't equal 0 then continue
3    -- Plates code
4end
0
Appreciated :) iFlusters 355 — 10y
Ad

Answer this question