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

How Do I Remove a Part Every Second?

Asked by 9 years ago

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.

0
Please Look at my answer again, I made an even SHORTER version of the script. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

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()
0
You should of just done a in pairs loop. EzraNehemiah_TF2 3552 — 9y
0
You also could of done AddItem(myTable[index], 1) instead of myTable[index]:Destroy() wait(1). Just tips to make scripts shorter! EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

Answer this question