Cannot destroy a part on Client

So I was trying to delete some server-sided preload part on a client. It doesn’t seem to work because the parts refused to be destroyed.

local WhiteList = workspace:WaitForChild("WhiteListTarget"):WaitForChild("NEWMAP")
script.Parent.Activated:Connect(function()
    for i,child in pairs(WhiteList:GetDescendants()) do
        if child:IsA("BasePart") or child:IsA("UnionOperation") then
            if not child.CanCollide and child.Transparency ~= 1 then
             child:Destroy()
            end
        end
    end
end)

could you please reformat your code

have you already tried print debugging to see if the code even makes it to the destroy?

It gave me a warning and set the parts back to their current parent.

What is the warning exactly?

(Had to type this because the forum has a minimum character limit)

1 Like

Something unexpectedly tried to set the parent of Leaves to NULL while trying to set the parent of Leaves. Current parent is BigPineTree2.

Hmm, does any other part of your code deal with destroying objects or parenting objects similar to the one you posted?

No, but I have StreamingEnabled and Deferred Signal Behavior. I do have some DescendantAdded Events, but none of it is trying to set the parent of those parts.

Maybe… you meant :GetChildren() and not :GetDescendants() because it returns all the children of the parent and not the children of the objects under the parent?

I did check if they are BasePart though, so it is very unlikely to be the parent.

Could you send me a picture of the parts you are trying to destroy and the parent too? Theres nothing wrong with the code, maybe its something to do with the logic.

Well, I’m just trying to optimize the game on performance mode by destroying useless parts that have nothing to do with gameplay. It’s the plants and the trees.

No, not in game but in workspace…

Try debugging and see which part of your code is not functioning… or even check you are using the Activated event on the right instance…

The button was for debugging. I mainly use another DescendantAdded Event to check each part to destroy since my game is StreamingEnabled, parts are only loaded when they neared.

Maybe theres objects being created and then you are trying to destroy it immediately after it being created? Try adding a wait(0) or wait(1) before destroying each object.

1 Like

Yeah, it worked. I used task.defer then destroy it, and the parts are not being created. They were already there on the server though.

1 Like

Is task.defer used over wait()? Is wait() deprecated?

maybe try a repeating wait Until the part your trying to destroy has a parent then once the parent is found then try to delete it.

I think task.defer schedule the thread to run quicker than wait() or task.wait() since those makes you wait a heartbeat, and the defer is more like schedule to run at the end of the current thread.
wait() is definitely deprecated. use task.wait or task.defer

1 Like

Oh, interesting, task.defer() seems to be an improved version of spawn and task.wait() seems to resume the thread faster than wait().

Thanks anyway!