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

Does anyone know how to fix this part cleaner script for my friend?

Asked by 7 years ago

What it's supposed to do is clean everything in the Workspace named "Part" but it doesn't work properly, here's the script.

print("CLEANUP LOADED!!")

while true do wait(5) print("Bye bye bricks")

local found = game.Workspace:FindFirstChild("Part")
    if found then 
        game:GetService("Debris"):AddItem(found,3)
    end    

print("Cleaned up!")

end

0
Fix your code block Goulstem 8144 — 7y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Problem

Seems this does work, but the program is waiting 5 seconds in between destroying bricks.

Lower the yield on line 1 and you should be fine.

while true do wait() --Lowered yield in wait function = faster iterations
    print("Bye bye bricks")
    local found = game.Workspace:FindFirstChild("Part")
    if found then 
        game:GetService("Debris"):AddItem(found)
    end    
    print("Cleaned up!")
end

Revisions

Now, this code is functional, but it isn't efficient yet. There are some syntax changes that could be made:

  • 1) while loops check a condition prior to executing code. Instead of having true in the conditional spot, you can put the wait function there.

  • 2) This loop is going to run forever, constantly checking for Parts even if there are none. You can either add a break statement(which breaks loops) or you can put a compound conditional in the while loop. This would consist of the and statement, along with another conditional - being workspace:FindFirstChild("Part") to ensure the loop only runs while parts are in the workspace.

  • 3) With this low of a yield between iterations printing anything is going to spam your output. You should remove prints.

while wait() and workspace:FindFirstChild("Part") do
    --Since you already know a Part exists, no need to check.
    game:GetService("Debris"):AddItem(workspace.Part)
end
Ad

Answer this question