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

How Do I Remove a Part Every Second?

I'm trying to make a script where I can remove one part per second but it's not working.

I tried doing this:

game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
game.Workspace.Part:remove
Wait (1)
end

But it didn't work so please help.

1 comment

0

Please Look at my answer again, I made an even SHORTER version of the script.

2 answers

✓ Accepted answer

You could loop through the workspace in search for all children with the Name Part or ClassName BasePart.

Loops facilitate simultaneously working with descendants of models or instances. Using one in this context would reduce the amount of lines the used by your code, and is overall much more efficient.

Numeric for

local myTable = workspace:GetChildren()
for index = 1, #myTable do -- 1 being the interval, and the table after the comma.
	-- now check if the part at `index`'s Name is equal to Part
	-- and also check that the part and `index` is a `BasePart`
	if myTable[index].Name == 'Part' and myTable[index]:IsA('BasePart') then
		myTable[index]:Destroy() -- call the destroy function of part at `index`
		wait(1) -- wait 1 second
	end
end

If you want it to be an infinite check, you could always make it all under another loop, that would run infinitely, instead of based on the number of parts inside the table/model.

Infinite loop

local myTable = workspace:GetChildren()

while wait(1) do
	for index = 1, #myTable do -- 1 being the interval, and the table after the comma.
		-- now check if the part at `index`'s Name is equal to Part
		-- and also check that the part and `index` is a `BasePart`
		if myTable[index].Name == 'Part' and myTable[index]:IsA('BasePart') then
			myTable[index]:Destroy() -- call the destroy function of part at `index`
		end
	end
end

Finally, you could make a function to run whenever called, so that all you'd have to do to clear the workspace of all parts is to call the function

Function

local myTable = workspace:GetChildren()

clearWorkspace = function()
	for index = 1, #myTable do -- 1 being the interval, and the table after the comma.
		-- now check if the part at `index`'s Name is equal to Part
		-- and also check that the part and `index` is a `BasePart`
		if myTable[index].Name == 'Part' and myTable[index]:IsA('BasePart') then
			myTable[index]:Destroy() -- call the destroy function of part at `index`
			wait(1) -- wait 1 second
		end
	end
end

clearWorkspace()

2 comments

0

You should of just done a in pairs loop.

0

You also could of done AddItem(myTable[index], 1) instead of myTable[index]:Destroy() wait(1). Just tips to make scripts shorter!

I know ImageLabel's script works but please read the following message: THERE IS A SIMPLER AND MORE EFFICIENT WAY TO SCRIPT THIS!


I can turn ImageLabel's 14 lined script into a 7 lined one.

for _,p in pairs(workspace:GetChildren()) do
	if p:IsA("BasePart") and
		p.Name == "Part" then
		p:Destroy()
	end
	wait(1)
end


Hope this helps!




5 Lined version:

for _,p in pairs(workspace:GetChildren()) do
	if p:IsA("BasePart") and p.Name == "Part" then
		game:GetService("Debris"):AddItem(p, 1)
	end
end

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