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

Loop script not working?

Asked by 8 years ago
Edited 7 years ago

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

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

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
0
You did manage to make it run forever but "UFO" can still be in workspace and it doesn't get removed. Precisionly 103 — 8y
0
@precisionly Try removing the ;, Lua doesn't need them Shawnyg 4330 — 8y
0
I instead added while true do and it works fine now. Precisionly 103 — 8y
0
@Precisionly That's great. Could you accept? Shawnyg 4330 — 8y
View all comments (2 more)
1
I'd like to mention this is extremely inefficient, you're looping through every object in the workspace hundreds of times a second. Nauseating 125 — 8y
0
@Nauseating I did what he asked for, never said it was efficient. Shawnyg 4330 — 8y
Ad

Answer this question