Re-adding storage items upon spawn after death. Can anyone please help?

Hey there,

I’ve created a custom inventory system. In a nutshell: an item icon becomes visible if that specific item gets added to the player’s ‘storage’ folder. It becomes invisible when the item is removed.

The problem with this is what happens when a player dies: the player respawns with the items in their storage, but the game doesn’t count it as items being added, so the icons stay invisible.

To solve this problem, I wanted to make a script that deletes all items inside the player’s storage folder, and re-adds them when the player spawns. However, the script below won’t work as the ‘gear’ variable isn’t available anymore. Can anyone have a look and please help? I’d rather not rewrite my inventory system itsself either, but any help is appreciated, as I’ve been dwelling on this problem for quite a while now.

I’ve tried all manners of scripts (such as an event firing when the player dies and respawns), but to no avail.

	player.CharacterAdded:Connect(function(character)
		
		character:WaitForChild("Humanoid").Died:Connect(function()

			local items = player.Storage:GetChildren();

				for i=1,#items do
					local gear = items[i]:Clone()
				items[i]:Destroy()				
			end
			wait(game.Players.RespawnTime + 0.25)
			gear.Parent = player.Storage -- this won't work
		end)
	end)
end)
-- re add items script

Additionally, here is the script for the inventory icons.

----- visibility script
script.Parent.Visible = false

game.Players.LocalPlayer.Storage.ChildAdded:Connect(function(item)
if item.Name == script.Parent.Name then
script.Parent.Visible = true
	end
end)	

game.Players.LocalPlayer.Storage.ChildRemoved:Connect(function(item)
	if item.Name == script.Parent.Name 
	and not game.Players.LocalPlayer.Storage:FindFirstChild(script.Parent.Name) then
	script.Parent.Visible = false
	end	
end)

Thanks in advance and have a lovely day!