How do i make items save after death?

Title is a bit self explanatory, i want to make a script to save a player’s inventory after they die. Basically how to get your tools back after you die.

I hope i was able to explain it, every help is appreciated, thanks. :slightly_smiling_face:

2 Likes

The player has an especific place to store the tools that are permanent (StarterGear):
image

If you want to make so tools from the StarterPack go to the StarterGear just do:

local Players = game:GetService("Players")
local Client = Players.LocalPlayer

for i, obj in pairs(Client.Backpack:GetChildren()) do
	if obj:IsA("Tool") then
		local NewTool = obj:Clone()
		
		NewTool.Parent = Client.StarterGear
	end
end
2 Likes

That would be in a localscript, correct?

1 Like

Yes, because you are localizing the player.

1 Like

Ok, thanks!

I’m gonna test it out.

Ok so the results of the testing weren’t really good.

To start off, all items in StarterPack started to duplicate after each death:
image

And also the script itself didn’t work, when i got new items they didn’t come back after death. I mean, they weren’t put in Player.StarterGear

1 Like

Well, there are a few thing about what he is trying to do.

  1. Putting items in StarterGear locally would not replicate it to the server, so idk why he recommended that.
  2. Clear the StarterGear each time they die then clone it. Of course, on the server.
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			plr.StarterGear:ClearAllChildren()
			for i,v in pairs(plr.Backpack:GetChildren()) do
				v:Clone().Parent = plr.StarterGear
			end
		end)
	end)
end)
2 Likes

My bad, I didn’t kept in mind that. I remember you should put Backpack tools on StarterGear to make them respawn, but I’m not really sure how does this exactly work cause I don’t use this so often.

It did give the items back after death but i still got the items in StarterPack

image

Note: Beans, Water and Revolver are on StarterPack and Lifesteal was the one i got in game and received back after death.

So basically it is giving the items back after death but they keep multiplying every death
image

1 Like

I’m not sure why you don’t just tick on the property “KeepInventoryOnDeath” located under Starterpack.

3 Likes

In the script I gave, it does clear the children of the StarterGear, so I don’t know what the problem is. Try clearing all of the children after the player’s character spawns.

image

Oh nvm. I thought I saw it before.

1 Like

Are your weapons currently located in StarterPack? If so, maybe that’s why weapons in the Backpack are duplicating…?
Can you please be more specific on what you are trying to achieve? (I know you want weapons that are in the Backpack to save, but there is already StarterPack that saves your weapons automatically after death, unless you are trying to do something else)

Ooh ok.

There are already some items in the StarterPack, but the player is able to buy more items in-game, and i want to make it so those items that the player bought are still avaiable to use after death, so besides having items that are already in the starterpack, i want it to save other items that aren’t in it.

Did i explain it correctly or did you not understand something?

2 Likes

Okay, I understand now. When players purchase a weapon, you want the weapon to be placed in both Backpack AND StarterGear (just edited it, my bad). You dont need to do anything for the weapons that are currently in StarterPack.
Code example:

Button.Activated:Connect(function()
   Weapon:Clone().Parent = player.Backpack
   Weapon:Clone().Parent = player.StarterGear
end)

This is just an example. You should not parent the weapons Client-Side. Do it Server-Side by firing a remote event after a player clicks or taps on the purchase button, and then do the parenting on the Server Script.
Another Code Example:

-- local script
Button.Activated:Connect(function()
   game.ReplicatedStorage.WeaponPurchased:FireServer()
end)

-- Server Script
game.ReplicatedStorage.WeaponPurchased.OnServerEvent:Connect(function(player)
   -- Im not sure where you weapons are located, so you do the stuff
   Weapon:Clone().Parent = player.Backpack
   Weapon:Clone().Parent = player.StarterGear
end)
3 Likes

Alright, now this all makes sense, I was wondering what was wrong with my script, but now I can edit the script to work:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			plr.StarterGear:ClearAllChildren()
			for i,v in pairs(plr.Backpack:GetChildren()) do
				if game.StarterPack:FindFirstChild(v.Name) then return end
				v:Clone().Parent = plr.StarterGear
			end
		end)
	end)
end)

Also, funny thing is while I was typing this like 10 mins ago, my power went out and I had to restart my pc

4 Likes

Oh, ok!

I will take a look at it tomorrow because it’s very late for me right now and my internet is very bad so i can’t open studio.

Thanks!

1 Like

I managed to join studio.

It’s not working :slightly_frowning_face:

Edit: I’m gonna mark what works the best as solution but if anyone else comments a better one i will change it.

1 Like

were there any errors in the output?

1 Like