Delete all tools in inventory Apon death problem [SP]

Hello Developers,

I am creating a script to delete all items from a player’s StarterGear/ Backpack. The reason why I
want to do this is, for one, I have a Pay-to-Get-In ‘Healing Room.’ You need it so you don’t die and
lose your items. And two: A shop in my game brings a tool to the players StarterGear whenever
you buy something. (I tried changing it to the “Backpack,” but it never worked. And even then,
you’ll still spawn with it after death.)

Where I’m at now:
I currently have a Local Script inside of StarterPlayerScripts that is supposed to delete the items. Before I show you the script, there are three issues I already know. One: The script runs on a “While Wait() Do” statement, Looping the script even after the player respawns. Two: I’ve tried taking out the loop factor of the script, but it only worked once, Meaning it worked the first time a player died but not after. And three: I am not an expert coder by any means, so it’s all skin and bones. Okay, here’s the script:

local char = game.Workspace:WaitForChild(player.Name)
local hum = char.Humanoid

hum.Died:Connect(function()
	while wait() do
	for i, v in ipairs(script.Parent:GetChildren()) do
		if v:IsA("Tool") then
			v:Destroy()
		end
	end

	for i, v in ipairs(player.Backpack:GetChildren()) do
		if v:IsA("Tool") then
				v:Destroy()
				break
		else --<<
			for i, v in ipairs(script.Parent:GetChildren()) do
				if v:IsA("Tool") then
					v:Destroy()
				end
			end

			for i, v in ipairs(player.Backpack:GetChildren()) do
				if v:IsA("Tool") then
					v:Destroy()
				end
			end
		end
	end
end
end)

11 Likes

So to clarify you want all tools in startergear to be gone on death?

3 Likes

Yes… Now, the script does do that, but whenever you try to buy something, it doesn’t show in your inventory.

3 Likes

maybe when the player dies the script runs in a loop. so try removing the while wait() part

2 Likes

I did, but it only worked once. Meaning, it worked the first time a player died, but not the second.

2 Likes

can u send me ur current script? or is it the same as the other one

2 Likes

its the same one that I linked in post.

so your goal here is to loop through all the tools in backpack and startergear to delete it?

1 Like

After death, yes

(Character limit)

1 Like

You need to remove while wait do, your script wont work otherwise

1 Like

when the player dies the backpack is already cleared so only the StarterGear needs to get deleted, right?

(server script btw)

game.Players.PlayerAdded:Connect(function(player)
	local character = player.CharacterAdded:Wait()
	local hum = character:WaitForChild('Humanoid')
	
	hum.Died:Connect(function()
		for i, v in pairs(player.StarterGear:GetChildren()) do
			if v:IsA('Tool') then
				v:Destroy()
			end
		end
	end)
end)
2 Likes

I noticed that its on starter player, have you tried starter character? @minecraftwolf1978

1 Like

Yes, because if the loop is removed, that is the only thing remaining. Now, with the loop, it does delete those items, but you can’t buy anything anymore…

1 Like

Yes I have,and nothing happened saddly.

1 Like

This is weird, are there any scripts interfering? And are you sure the shop gives it in the first place, you should add prints when the for i,v destroys smth to see if its really the problem

1 Like

have you tried converting it to a serverScript?

1 Like

Yes that too, it wont work if its on local, try a remote event

This is probably your problem because the server hasnt registered that the tools were given to you

The shop was working. It does give you stuff in the Startergear, but it doesn’t show up because of the loop

Yes but this wouldnt be a problem tho, is it local or server