Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I keep a player's items when they die?

I have the tool save set up and everything but the one remaining issue is that I can't get the player to keep their purchased weapons (those not in the starter pack) when they die. When the player respawns they will have an extra starting weapon for each time they died. For example, if I have an MP7 in my hand when I die, that MP7 will be gone on respawn and there will be another starter pistol. If I die again, the purchased weapon disappears and ANOTHER pistol gets added, which means on respawn the player will have a bunch of pistols but no weapons they actually bought. I'll show the whole script but there's one part I'm almost certain is the reason, unless I'm missing something else.

local ss = game:GetService("ServerStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("ownedTools")
local Tools = ss:WaitForChild("Tools")


local dir = {}

local function edit(player, list)
	dir[player.UserId] = list
end

local function setup(player, list)
	for i = 1, #list do
		local tool = Tools:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		else
			print(list[i].." not found")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	
	local ready = false
	
	player.CharacterAdded:Connect(function(char)
		
		local bp = player.Backpack
		local data = nil
		
		if ready == false then
			ready = true
			
			data = store:GetAsync(player.UserId)
			
			if data then
				setup(player, data)
				edit(player, data)
			end
		end
		
		char.Humanoid.Died:Connect(function()
			char.Humanoid:UnequipTools()
			
			local old = player.StarterGear:GetChildren()
			for i = 1, #old do
				old[i]:Destroy()
			end
			
			local new = player.Backpack:GetChildren()
			for i = 1, #new do
				new[i].Parent = player.StarterGear
			end
		end)
		
		local count = 0
		local function adjust()
			
			if char.Humanoid.Health > 0 then
				
				local list = {}
				local equipped = char:FindFirstChildOfClass("Tool")
				if equipped then 
					table.insert(list, equipped.Name)
				end
				
				local tools = bp:GetChildren()
				for i = 1, #tools do
					table.insert(list, tools[i].Name)
				end
				
				if count ~= #list then
					edit(player, list)
					count = #list
				end
			end
		end
		
		bp.ChildAdded:Connect(adjust)
		bp.ChildRemoved:Connect(adjust)
		
		char.ChildAdded:Connect(function(child)
			if child.ClassName == "Tool" then
				adjust()
			end
		end)
		
		char.ChildRemoved:Connect(function(child)
			if child.ClassName == "Tool" then
				adjust()
			end
		end)
		
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	store:SetAsync(player.UserId, dir[player.UserId])
	dir[player.Name] = nil
end)

game:BindToClose(function()
	wait(5)
end)

This is the entire script. Again, the save and load work, it's this next part below that I believe is giving me trouble.

char.Humanoid.Died:Connect(function()
			char.Humanoid:UnequipTools()
			
			local old = player.StarterGear:GetChildren()
			for i = 1, #old do
				old[i]:Destroy()
			end
			
			local new = player.Backpack:GetChildren()
			for i = 1, #new do
				new[i].Parent = player.StarterGear
			end
		end)

This is supposed to loop through and give the player back what they had when they died but it is destroying whatever is being HELD and adding another starterGear weapon. If the player is NOT holding a purchased weapon when they die, the purchased weapon stays in the backpack on respawn. This is giving me more trouble than anything else thus far. This is my first game so any help is appreciated!

1 comment

0

I have solved the problem. If anyone is reading this and is curious what I did... I am not really sure lol. I made some changes here and there until it worked. Here is the working script.

1 answer

Here is the working script. I had to go into the weapon server scripts (not shown) to make a whole second clone to put into the player's startergear so that when the weapon is purchased I can pull the clone from the startergear and give a clone when the player respawns. So I am giving one clone on the buy event and the actual "owned" clone goes into the startergear. That owned clone can then be replicated over and over on each death to give back to the player when they respawn. Three weeks ago I would have never thought I'd still be working on this. Proud of myself for this one!

local ss = game:GetService("ServerStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("ownedTools")
local Tools = ss:WaitForChild("Tools")


local dir = {}

local function edit(player, list)
	dir[player.UserId] = list
end

local function setup(player, list)
	for i = 1, #list do
		local tool = Tools:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
			print("added "..clone.Name)
		else
			print(list[i].." not found")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)	
	player.CharacterAdded:Connect(function(char)
		
		local bp = player.Backpack
		local data = nil
		
		
			
			data = store:GetAsync(player.UserId)
			
			if data then
				setup(player, data)
				edit(player, data)
			end
		
		char.Humanoid.Died:Connect(function()
			char.Humanoid:UnequipTools()
			
			local new = player.StarterGear:GetChildren()
			for i = 1, #new do
				new[i].Parent = player.Backpack
				print("added "..new[i].Name)
			end
		end)
		
		local count = 0
		local function adjust()
			
			if char.Humanoid.Health > 0 then
				
				local list = {}
				local equipped = char:FindFirstChildOfClass("Tool")
				if equipped then 
					table.insert(list, equipped.Name)
				end
				
				local tools = bp:GetChildren()
				for i = 1, #tools do
					table.insert(list, tools[i].Name)
				end
				
				if count ~= #list then
					edit(player, list)
					count = #list
				end
			end
		end
		
		bp.ChildAdded:Connect(adjust)
		bp.ChildRemoved:Connect(adjust)
		
		char.ChildAdded:Connect(function(child)
			if child.ClassName == "Tool" then
				adjust()
			end
		end)
		
		char.ChildRemoved:Connect(function(child)
			if child.ClassName == "Tool" then
				adjust()
			end
		end)
		
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	store:SetAsync(player.UserId, dir[player.UserId])
	dir[player.Name] = nil
end)

game:BindToClose(function()
	wait(3)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.