Destroy tools on rebirth

Hi. I tried to make the players tools be destroyed when I rebirth and the first tool to be cloned in the players backpack (second thing works) for a simulator game. This is code I tried:

replicatedStorage.Remotes.Rebirth.OnServerInvoke = function(Player)
	if not remoteData:FindFirstChild(Player.Name) then return "NoFolder" end
	
	local rebirths = Player.leaderstats.Rebirths
	local firstpc = game.ServerStorage.Tools["First Pc"]
	local clone = firstpc:Clone()
	
	if Player.leaderstats.Hackables.Value >= (math.floor((starterRebirthAmount + (rebirths.Value) * math.sqrt(999)))) then
		
		for _,v in pairs(game.ServerStorage.PlayerTools[Player.Name]:GetChildren()) do
			if v:IsA("Tool") then
				v:Destroy() --Destroyes the tools that were already bought(this works)
			end
		end
		Player.Character.Humanoid:UnequipTools()
		for i, tool in pairs(Player.Backpack:GetChildren()) do
			if tool.Name == Player.Equipped.Value then
				tool:Destroy() --I want this to destroy the tools in the inventroy
			end
		end
		rebirths.Value = rebirths.Value + 1
		Player.leaderstats.Hackables.Value = 0
		Player:LoadCharacter()
		clone.Parent = Player.Backpack
		
		return true
		else return "NotEnoughCodes"
	end
end

But when I try to rebirth it clones the first pc in my inventory + the tool that was in my inventory before I rebirthed.

For more information, I made a folder which contains the bought tools so you can equip them in the shop. When you rebirth it deletes them so you need to buy them again. but I want the players backpack to also be deleted.

(P.S I probably won’t react in a few hours because of personal things)

You also have to account for the Player’s StarterGear, cause every time the Character respawns the tools inside the StarterGear would be cloned into their Backpack as well

Before you call LoadCharacter(), you should also loop through the Player’s StarterGear & remove everything inside there that’s a Tool:

replicatedStorage.Remotes.Rebirth.OnServerInvoke = function(Player)
	if not remoteData:FindFirstChild(Player.Name) then return "NoFolder" end

	local rebirths = Player.leaderstats.Rebirths
	local firstpc = game.ServerStorage.Tools["First Pc"]
	local clone = firstpc:Clone()

	if Player.leaderstats.Hackables.Value >= (math.floor((starterRebirthAmount + (rebirths.Value) * math.sqrt(999)))) then

		for _,v in pairs(game.ServerStorage.PlayerTools[Player.Name]:GetChildren()) do
			if v:IsA("Tool") then
				v:Destroy() --Destroyes the tools that were already bought(this works)
			end
		end
		Player.Character.Humanoid:UnequipTools()
		
		for i, tool in pairs(Player.Backpack:GetChildren()) do
			if tool.Name == Player.Equipped.Value then
				tool:Destroy() --I want this to destroy the tools in the inventroy
			end
		end
		
		for _, tool in pairs(Player.StarterGear:GetChildren()) do
			if tool:IsA("Tool") then
				tool:Destroy()
			end
		end
		
		rebirths.Value = rebirths.Value + 1
		Player.leaderstats.Hackables.Value = 0
		Player:LoadCharacter()
		clone.Parent = Player.Backpack

		return true
	else return "NotEnoughCodes"
	end
end

That didn’t work, I think it has something to do with another script adding the tools to the backpack.

game.ReplicatedStorage.Remotes.CreateTransaction.OnServerInvoke = function(player,item)
	local money = game.Players:FindFirstChild(player.Name).leaderstats.Hackables
	local cost = game.ServerStorage.ToolModels[item].Information.Cost
	
	if money.Value >= cost.Value and not game.ServerStorage.PlayerTools[player.Name]:FindFirstChild(item) then
		
		money.Value = money.Value - cost.Value
		player.Character.Humanoid:UnequipTools()
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool.Name == player.Equipped.Value then
				tool:Destroy()
			end
		end
		
		player.Equipped.Value = item
		
		local tool = game.ServerStorage.Tools:FindFirstChild(item):Clone()
		tool.Parent = game.ServerStorage.PlayerTools[player.Name]
		
		local tool = game.ServerStorage.Tools:FindFirstChild(item):Clone()
		tool.Parent = player.Backpack
		
		return true
		
	elseif cost.Value > money.Value and not game.ServerStorage.PlayerTools[player.Name]:FindFirstChild(item) then
		
		return "not enough cash"
	else
		return "already bought"
	end
	
end

game.ReplicatedStorage.Remotes.GetToolsBought.OnServerInvoke = function(player)
	
	local tools = {}
	
	for i, tool in pairs(game.ServerStorage.PlayerTools[player.Name]:GetChildren()) do
		table.insert(tools,tool.Name)
	end
	
	return tools
	
end

game.ReplicatedStorage.Remotes.EquipTool.OnServerEvent:Connect(function(player,toolName)
	
	player.Character.Humanoid:UnequipTools()
	
	if player.Equipped.Value ~= nil then
		if player.Backpack:FindFirstChild(player.Equipped.Value) then
		 player.Backpack[player.Equipped.Value]:Destroy()
		end
	end
	
	player.Equipped.Value = toolName
	
	local tool = game.ServerStorage.Tools:FindFirstChild(toolName):Clone()
	tool.Parent = player.Backpack
	
end)

This is the script that clones the tool to the backpack and destory’s the last equipped tool. Do I need to do something else here?

Nvm I fixed the code. The code I’m using now is:

replicatedStorage.Remotes.Rebirth.OnServerInvoke = function(Player)
	if not remoteData:FindFirstChild(Player.Name) then return "NoFolder" end

	local rebirths = Player.leaderstats.Rebirths
	local firstpc = game.ServerStorage.Tools["First Pc"]
	local clone = firstpc:Clone()

	if Player.leaderstats.Hackables.Value >= (math.floor((starterRebirthAmount + (rebirths.Value) * math.sqrt(999)))) then
		
		rebirths.Value = rebirths.Value + 1
		Player.leaderstats.Hackables.Value = 0
		Player.leaderstats.Codes.Value = 0
		Player:LoadCharacter()
		
		wait()
		
		for _,v in pairs(game.ServerStorage.PlayerTools[Player.Name]:GetChildren()) do
			if v:IsA("Tool") then
				v:Destroy()
			end
		end
		Player.Character.Humanoid:UnequipTools()
		for i, tool in pairs(Player.Backpack:GetChildren()) do		

			if tool:IsA("Tool")then	

				tool:Destroy()		
			end
		end




		clone.Parent = Player.Backpack

		return true
	else return "NotEnoughCodes"
	end
end

I used AlvinBlox’s code for the foundation

Thank you for helping though!