Have problem with debris

I have problem to delete parts when player died. Here is an example:

-- Function

local clonedBackground = background:Clone()
			clonedBackground.Name = "localBackground"
			clonedBackground.CFrame = character.PrimaryPart.CFrame * CFrame.new(0, 0, 2)
			clonedBackground.Rotation = character.PrimaryPart.Rotation
			clonedBackground.Parent = workspace
			
			local cfloor = floor:Clone()
			cfloor.Parent = workspace
			cfloor.Name = "LocalFloor"
			
			local discC = disc:Clone()
			disc.Name = "LocalDisc"
			discC.Parent = workspace
			
			cloneddiscs[player] = discC
			clonedfloors[player] = cfloor
			clonedBackgrounds[player] = clonedBackground

			humanoid.Died:Once(function()
				debris:AddItem(clonedBackgrounds[player], 0)
				clonedBackgrounds[player] = nil
				debris:AddItem(cloneddiscs, 0)
				cloneddiscs[player] = nil
				debris:AddItem(clonedfloors, 0)
				clonedfloors[player] = nil
			end)
-- rest code

debris:AddItem(clonedBackground, 0)
			debris:AddItem(discC, 0)
			debris:AddItem(cfloor, 0)

I don’t understand what problem you exactly have. Can you describe the problem?

Show more of your code, it helps others determine what’s causing the issues and seeing as there’s many variables missing; it’s hard to fix the coding.

To use the debris, it’s game.Debris:AddItem(Item,Time). Here’s the documentation of Debris, I recommend reading it.

Something else to note is that debris is old and actually slower than using task.delay and :Destroy()

1 Like

I want to make if player died, parts destroy.

But debris is not deleting all parts

local background = workspace.BloomBackground
local players = game:GetService("Players")

local clonedBackgrounds = {}

game:GetService("ReplicatedStorage").AmberS1.OnServerEvent:Connect(function(player)
	if player.Character then
		local tool = player.Character:FindFirstChild("Magic Finix Amber")

		if tool then
			local character = player.Character
			local humanoid = character.Humanoid

			local clonedBackground = background:Clone()
			clonedBackground.Parent = workspace

			clonedBackgrounds[player] = clonedBackground

			humanoid.Died:Once(function() -- Add things you want to destroy when player dies/resets in here
				clonedBackgrounds[player]:Destroy()
			end)

			clonedBackgrounds[player]:Destroy()

			humanoid.WalkSpeed = 16
			humanoid.JumpHeight = 7.2
			character.PrimaryPart.Anchored = false


			tool:Destroy()
		else
			warn("Magic Finix Amber isn't equipped!")
		end
	else
		warn("player.Character is currently nil")
	end
end)

game.ReplicatedStorage.ChangePlayerCharacter.OnServerEvent:Connect(function(player)
	clonedBackgrounds[player]:Destroy()
end)

players.PlayerRemoving:Connect(function(player)
	clonedBackgrounds[player]:Destroy()
end)

I used normal :Destroy but when i use tool few sec after again it deletes earlier.