Help With Saving Tools

Hello, I need help with a system that will save a players tools when they leave, but this script doesnt work,

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("BackPackService")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = datastore:GetAsync("User_"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolFound = game.ReplicatedStorage.Tools:FindFirstChild(v)
				if toolFound then
					toolFound:Clone().Parent = player.BackPack
					toolFound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolsave = {}
		for i,tool in pairs(player.BackPack:GetChildren())do
			if tool then
				table.insert(toolsave,tool.Name)
			end
		end
		datastore:SetAsync("User_"..player.UserId,toolsave)
	end)
end)

If you could help me fix this script, or help me find a easier or better method to save peoples tools please do, thank you!

1 Like

Change your code to this:

local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
 local key = "id-"..plr.userId
 pcall(function()
  local tools = ds:GetAsync(key)
  if tools then
   for i,v in pairs(tools) do
    local tool = game.ServerStorage.Tools:FindFirstChild(v)
    if tool then
     tool:Clone().Parent = plr:WaitForChild("Backpack")
     tool:Clone().Parent = plr:WaitForChild("StarterGear")
    end
   end
  end
 end)
end)
game.Players.PlayerRemoving:connect(function(plr)
 local key = "id-"..plr.userId
 pcall(function()
  local toolsToSave = {}
  for i,v in pairs(plr.Backpack:GetChildren()) do
   if v then
    table.insert(toolsToSave,v.Name)
   end
  end
  ds:SetAsync(key,toolsToSave)
 end)
end)

and add a folder in server storage and name it “Tools” and add the weapons you want to save.

Alright thx man il go test it out.

1 Like

Doesnt seem to work, I dont know if this is due to that the players not spawning in with the item, but that the player buys the item then it needs to save.

Have you put it in serverscriptservice and have you activated Api services?

Yes, those were done., can you take a look at it for me? Heres a copy Give.rbxl (67.7 KB)

It works perfectly for me, do you leave some time to save?

Join me in this and il show you Something... - Roblox