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

How can I loop a script?

Asked by 8 years ago

How would I loop this script so it always runs? Basically, every time it finds something named "UFO" inside workspace, it will be removed.

game.Workspace.ChildAdded:connect(function(child)
    if child.Name == "UFO" then
        child:Destroy()
    end
end)

2 answers

Log in to vote
0
Answered by 8 years ago

That'll be fine. Just stick a for loop after it to get rid of any existing UFO things.

for k,v in pairs(game.Workspace:GetChildren()) do
    if v.Name == 'UFO' then
        v:Destroy();
    end;
end;

You don't need to loop through it every time a Child is added, because the only change will be the new Child - Which will be removed by your existing code.

0
I tried this by using my stamper tool and spawning a UFO and the script didn't remove it. Any ideas why? Precisionly 103 — 8y
0
I put it into the developer console after I spawned the UFO's and then it removed the UFO's, I want it to remove as soon as it's in the game. Precisionly 103 — 8y
Ad
Log in to vote
0
Answered by
pwnd64 106
8 years ago

In order to do this we can use a for loop.

http://wiki.roblox.com/index.php?title=Loops

For everything the loop finds, it will do something. The code would look something like

game.Workspace.ChildAdded:connect(function(child)

    for int,v in pairs(game.Workspace:GetChildren()) do --iterates through the workspace and its children

        if v.Name == "UFO" then --checks if the current item it's checking is named ufo

            v:Destroy()

        end

    end --then it loops until it does it for everything

end)

in a for loop, int represents the integer (the number) that the loop is at. so if there's 3 children in Workspace int will get up to 3. v represents the current item that it's checking.

Hope that helped.

Answer this question