Tools not saving after a player dies

I made a script so that when a player dies their inventory gets saved and reloaded when they respawn. At first if a player was holding an item it wouldn’t save that item because it wasn’t in the players backpack but their character. So I fixed it but rarely if a player is holding an item it won’t save. I don’t know why it only works sometimes and not other times but here is the code.

local function OnDied() --Copies all your tools on death and saves them to a table attached to the player
			for Index, Tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = Tool:Clone()
				table.insert(SavedTools, CopiedTool)
			end

			for Index, Tool in pairs(Player.Character:GetChildren()) do --Checks if theres a tool being currently used/equipped by the player
				if Tool:IsA("Tool") then
					local CopiedTool = Tool:Clone()
					table.insert(SavedTools, CopiedTool)
				end
			end
		end

EDIT–
I just realized this rare glitch only happens when the player dies from falling off of the map. In this case it happens literaly every time.

I would recommend Parenting all the tools to a Folder or something then Parenting them back once the player respawned

Server Script in ServerScriptService

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Hum = Character:WaitForChild("Humanoid")
		
		Hum:GetPropertyChangedSignal("Health"):Connect(function()
			if (Hum.Health <= 0) then 
				local ToolFolder = Instance.new("Folder", game:GetService("ReplicatedStorage"))
				
				for i,v in pairs(Character:GetChildren()) do 
					if (v:IsA("Tool")) then 
						v.Parent = ToolFolder
					end
				end
				
				for i,v in pairs(Player.Backpack:GetChildren()) do 
					v.Parent = ToolFolder
				end
				
				Player.CharacterAdded:Wait()
				
				for i,v in pairs(ToolFolder:GetChildren()) do 
					v.Parent = Player.Backpack
				end
				
				ToolFolder:Destroy()
			end
		end)
	end)
end)

Yeah that’s pretty much what I am doing, I just showed the part of the code that I think is relevant.

im kinad busy right now so i can’t make a script but what i would do is:
check if the player has the tool, if he has the tool and he dies then clone the weapon back into his invetory when he respawns.

1 Like