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

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

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

1 comment

0

Fix your code block

1 answer

✓ Accepted answer

#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.
    1. 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.
    1. 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

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