Is there a way to save my tools on death/reset and leave/rejoin?

I need to have my tools save when the players respawn or leave the game

I have been using this script and it has been working on/off for the past few weeks.

local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("Stats")

local savetools = game.ReplicatedStorage.CurcWeapons

game.Players.PlayerAdded:Connect(function(plr)
	
	local savedItems = data:GetAsync(plr.UserId.."-Items")
	
	if savedItems then
		
		for i, item in pairs(savedItems) do
			
			for i, tool in pairs(savetools:GetChildren()) do
				
				if item == tool.Name then
					local toolClone1 = tool:Clone()
					toolClone1.Parent = plr.Backpack
					print("Cloned ", tool.Name)
					local toolClone2 = tool:Clone()
					toolClone2.Parent = plr.StarterGear
					print("Cloned Starter ", tool.Name)
					
				end
				
			end
			
		end
		
	end	
end)


game.Players.PlayerAdded:Connect(function(plr)

	local savedItems = data:GetAsync(plr.UserId.."-Items")

	if savedItems then

		for i, item in pairs(savedItems) do

			for i, tool in pairs(savetools:GetChildren()) do

				if item == tool.Name then
					local toolClone1 = tool:Clone()
					toolClone1.Parent = plr.Backpack
					print("Cloned ", tool.Name)
					local toolClone2 = tool:Clone()
					toolClone2.Parent = plr.StarterGear
					print("Cloned Starter ", tool.Name)
				end

			end

		end

	end

end)


game.Players.PlayerRemoving:Connect(function(plr)
	
	plr.CharacterRemoving:Connect(function(char)
		local humanoid = char.Humanoid
		humanoid:UnquipTools()
	end)
	
	local suc, err = pcall(function()
		
		local savedItems = {}
		
		for i, item in pairs(plr.Backpack:GetChildren()) do
			table.insert(savedItems, item.Name)
			print(savedItems, item.Name)
		end
		
		data:SetAsync(plr.UserId.."-Items", savedItems)
		
	end)
	
end)

I need to make “humanoid health changed” I think but I am not sure what line i need to put in it so it can save the items every time they die not just when leaving and rejoin.

1 Like

This script should save weapon’s when leaving, make new weapons keep on death, and load them back in when rejoining

local DatastoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ToolDataStore = DatastoreService:GetDataStore("tools")

local ToolRefrence = ReplicatedStorage.Tools

game.Players.PlayerAdded:Connect(function(player: Player)	
	local savedTools = nil
	local success, err = pcall(function()
		savedTools = ToolDataStore:GetAsync(player.UserId)
	end)
	
	if success then
		print("Tools Loaded!")
		print(savedTools)
	elseif err then
		warn(err)
	end
	
	local starterGear = player.StarterGear
	-- Give's tools based on the saved tool names 
	if savedTools then
		for toolName, _ in pairs(savedTools) do
			local toolTemplate = ToolRefrence:FindFirstChild(toolName)
			if toolTemplate then
				local newStarterGear = toolTemplate:Clone()
				newStarterGear.Parent = starterGear
			end
		end
	end
	
	-- When the player dies make sure that the unsaved weapons will still be in the backpack
	player.CharacterRemoving:Connect(function()
		for _,tool in pairs(player.Backpack:GetChildren()) do
			if starterGear:FindFirstChild(tool.Name) == nil then
				local clone = tool:Clone()
				clone.Parent = starterGear
			end
		end
	end)
	
	if not player:HasAppearanceLoaded() then
		player.CharacterAppearanceLoaded:Wait()
	end
	
	-- Add tools if not given by starter pack
	for _,tool in pairs(player.StarterGear:GetChildren()) do
		if player.Backpack:FindFirstChild(tool.Name) == nil then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		end
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local toolSave = {}

	for _, tool in pairs(player.StarterGear:GetChildren()) do
		if ToolRefrence:FindFirstChild(tool.Name) then -- Makes sure that the tool is able to be loaded back in
			toolSave[tool.Name] = true
		end
	end
	
	local success, err = pcall(function()
		ToolDataStore:SetAsync(player.UserId, toolSave)
	end)
	
	if success then
		print("Tools Saved!")
		print(toolSave)
	elseif err then
		warn(err)
	end

end)

game:BindToClose(function()
	if RunService:IsStudio() then
		task.wait(3)
	else
		task.wait(10)
	end
end)
2 Likes

Yeah this seems to make them appear in the output but i think parenting them to the player might be an issue?

local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("Stats")

local savetools = game.ReplicatedStorage.CurcWeapons

game.Players.PlayerAdded:Connect(function(plr)
	local savedItems = data:GetAsync(plr.UserId.."-Items")
	if savedItems then
		for i, item in pairs(savedItems) do
			local tool = savetools:FindFirstChild(i)
			if tool then
				local toolClone1 = tool:Clone()
				toolClone1.Parent = plr.Backpack
				print("Cloned ", tool.Name)
				local toolClone2 = tool:Clone()
				toolClone2.Parent = plr.StarterGear
				print("Cloned Starter ", tool.Name)
			end
		end
	end	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	plr.CharacterRemoving:Connect(function(char)
		local humanoid = char.Humanoid
		humanoid:UnequipTools()
	end)

	local suc, err = pcall(function()
		local savedItems = {}
		for i, item in pairs(plr.Backpack:GetChildren()) do
			table.insert(savedItems, item.Name)
			print(savedItems, item.Name)
		end

		data:SetAsync(plr.UserId.."-Items", savedItems)
	end)
end)