How to remove/destroy many Parts with a single button

Hello, I need help, I am trying to make a system that eliminates the parts that the player generates with a button, I already have it done, a button generates a Part on the Server, and that part contains a StringValue called “Owner” with the name from the player who generated it, very good, now I have another button that sends from the Client to the Server through an Event and that Server Script is responsible for eliminating the parts, the problem is that it does not delete anything and Print is not executed either


Client Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")

local Button = script.Parent

Button.MouseButton1Up:Connect(function()
	ReplicatedStorage.Events.RemoveOwnerParts:FireServer()
	SoundService.SoundLibrary.Sounds.RemoveSound1:Play()
end)

Server Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local GeneratedParts = workspace.GeneratedParts:GetDescendants()

ReplicatedStorage.Events.RemoveOwnerParts.OnServerEvent:Connect(function(plr)
	for _, OwnerPart in pairs(GeneratedParts) do
		if OwnerPart:IsA("BasePart") then
			if OwnerPart:WaitForChild("Owner").Value == plr.Name then
				OwnerPart:Destroy()
				print("Parts successfully removed")
			end
		end
	end
end)

The Part and String Value
image


-GeneratedParts its a Folder
-Plr its the player from the Client
-No, I do not get any error in the Output, the Client does send the Event successfully to the Server, the problem is in the Server Script

thanks

You’re getting the descendants before everything, instead get the descendants during the loop.

GeneratedParts:GetDescendants()
2 Likes

Thanks, i forgot that writing above only gets the descendants once and the parts are created after that

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.