How do i make items save after death?

i want to make a script to save a player’s inventory after they die. Basically you get your tool back after u die

6 Likes

Use the following script I copied from a topic on the DevForum similar to this one.

local Players = game:GetService("Players")

function OnPlayerAdded(Player)
	
	local SavedTools = {}
	
	local function OnCharacterAdded(Character)
		
		for Index, Tool in pairs(SavedTools) do --Loads all the saved tools from the player's last death
			Tool.Parent = Player.Backpack
		end
		
		SavedTools = {}
		
		local function OnDied() --Copies all your tools when you die 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
		
		Character.Humanoid.Died:Connect(OnDied)
		
	end
	
	Player.CharacterAdded:Connect(OnCharacterAdded)
	
end

for Index, Player in pairs(Players:GetChildren()) do --Redundency incase a player has joined before the event sets up
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)
20 Likes

use it in serverscriptservice right? and script

1 Like

Yes. Put it in ServerScriptService as a server script.

3 Likes

Sorry for replying so late, but it duplicates everything in my starterpack, what do I do?

I think you can make a line that checks if saved item’s name does not cover with any of tools in starter pack, tho I believe that after these 15 days you found the solution. If so then I leave this for future generations.

I haven’t tested yet but maybe this would work.

while true do

wait(0.5)

local tool = plr.Backpack:FindFirstChild(“toolname”)

if tool ~= nil then

  newtool = yourtool:clone()
  
  newtool.Parent = plr.Backpack
  
  else

end
end

very simple so i dont really expect it to work but i hope it does

make sure to define plr, the tool, and the findfirstchild part

1 Like

Coming into this discussion pretty late. What should I put for newtool, yourtool, and where should I put it?