I need a way to save the players tools on death, then give it back to them when they respawn
I simply cannot figure it out, I perfer to not use data stores, as this is a single-player game and I feel that would be unnecessary
i have tried, cloning players backpack to a folder in Replicated Storage, aswell as moving tools to that folder, then moving it back into the players backpack
looking for posts on the forum, i mostly just found things using data stores.
when a humanoid dies, you can make a check on the server begin and save all the tools equipped by doing
hum:UnequipTools()
local tools = player.Backpack:GetChildren()
player.Backpack:ClearAllChildren()
now you can then loop through the copy once the new character is added, you could do this by establishing a characteradded.Once event.
hum:UnequipTools()
local tools = player.Backpack:GetChildren()
player.Backpack:ClearAllChildren()
player.CharacterAdded.Once:Connect(function()
for _,tool in tools do
local clone = tool:Clone()
clone.Parent = player.Backpack
end
end)
Just because you need to store data it doesn’t mean you need to store it forever, or so other servers can access it. You can store the current player tools as data in-game, so they only save in that server during player reset. You can do this by implementing your own tool system on top of the Roblox tool system, that has a list of players and their current tools. On reset it simply gives them those tools back.
-- ServerScript in ServerScriptService
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = player
player.CharacterAdded:Connect(function(character)
for _, tool in pairs(inventory:GetChildren()) do
tool:Clone().Parent = player.Backpack
end inventory:ClearAllChildren()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function() humanoid:UnequipTools()
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
tool:Clone().Parent = inventory
end
end
end)
end)
end)
I made that for the same reason you were talking about. More of a studio test for me without using the datastore constantly while building the rest of the program. I was even shocked it works so well.