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

Loop script not working?

This script seems to work fine when I put it into the developer console. When I just put the script, save it, and play the game, the script doesn't remove the "UFO."

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

1 answer

✓ Accepted answer

The script is correct, however it'll only run once, when the game is started. If you want it to constantly remove all objects named UFO, you should put it in a while loop!

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

6 comments

0

You did manage to make it run forever but "UFO" can still be in workspace and it doesn't get removed.

0

@precisionly Try removing the ;, Lua doesn't need them

0

I instead added while true do and it works fine now.

0

@Precisionly That's great. Could you accept?

1

I'd like to mention this is extremely inefficient, you're looping through every object in the workspace hundreds of times a second.

0

@Nauseating I did what he asked for, never said it was efficient.

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