So when you die it gives you the tool back if you have it and if it's in the toolFolder it may give you double amount of the tool(s) you own
script:
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()
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 and also gives too many of 1 tool
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
