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

I'm trying to make a saving inventory system but when i die I loss my tools or gears (?)

So I was making an inventory system I finished it that if you leave the game with tools and join again then you'll get your tools back but when you die with the tools you lose the tools so how do I make it if you die you'll keep your tools here's the script

Script:

local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ReplicatedStorage.Items

game.Players.PlayerAdded:Connect(function(plr)
	local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
	for i, toolSaved in pairs(toolsSaved) do	
		if toolsFolder:FindFirstChild(toolSaved) then 		
			toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
			toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear 
		end
	end
	plr.CharacterRemoving:Connect(function(char)
		char.Humanoid:UnequipTools()
    end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	local toolsOwned = {}
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do	
		table.insert(toolsOwned, toolInBackpack.Name)
	end
	local success, errormsg = pcall(function()		
		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)

1 answer

✓ Accepted answer
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ReplicatedStorage.Items

game.Players.PlayerAdded:Connect(function(plr)
	local toolsSaved
	local s,e = pcall(function() -- added a pcall because there could be an error here too
		toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
	end)
	if s then
		plr.CharacterAdded:Connect(function(player) -- this gives the tools to the player once they have respawned, after they died, etc
			giveTools(plr, toolsSaved)
		end)
	    plr.CharacterRemoving:Connect(function(char)
	        char.Humanoid:UnequipTools()
		end)
	else
		warn(e)
	end
end)


game.Players.PlayerRemoving:Connect(function(plr)
    local toolsOwned = {}
    for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do   
        table.insert(toolsOwned, toolInBackpack.Name)
    end
    local success, errormsg = pcall(function()      
        toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
    end)
    if errormsg then warn(errormsg) end
end)

function giveTools(player, tools) -- just a function so you dont have to write it multiple times, not needed but cleans up code for later
	if player and player:IsA("Player") then -- checks if the given player is a "Player"
		for i,toolSaved in pairs(tools) do
			if toolsFolder:FindFirstChild(toolSaved) then       
	            toolsFolder[toolSaved]:Clone().Parent = player.Backpack
	            toolsFolder[toolSaved]:Clone().Parent = player.StarterGear 
	        end
		end
	end	
end

Gave comments on what I changed. If you need anymore help just comment here.

Note: I don't know if you should be saving data this way because if the player dies and leaves, they will have no more tools. Just my opinion.

2 comments

0

I'll make it so if the players die and leave then they will still have their tools thanks!

0

Ok, sounds good. Glad to help!

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