Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't my tool save if i equip it and then leave the game?

Asked by
Soban06 410 Moderation Voter
4 years ago

So I am making an Obby in which players can buy items from the shop. Once they buy the item, it saves in their backpack and if they leave the game, the item/items which they have also get saved and load into their backpack once they rejoin. But the problem is that if a player leaves the game while equipping an item, the next time they join the game, their item which was equipped is lost.

How to fix this?

I have a toolSave script inside ServerScriptService which is as follows:

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:FindFirstChild("Backpack")
     tool:Clone().Parent = plr:FindFirstChild("StarterGear")
    end
   end
  end
 end)
end)
game.Players.PlayerRemoving:connect(function(plr)
 local key = "id-"..plr.userId
  local toolsToSave = {}
  for i,v in pairs(plr.Backpack:GetChildren()) do
   if v then
    table.insert(toolsToSave,i,v.Name)
   end
  end
  ds:SetAsync(key,toolsToSave)

    local char = plr.Character   
    if char then
        local charParts = char:GetChildren()
        for key,part in (charParts) do
            if part.IsA("Tool") then
                table.insert(toolsToSave,part)
            end
        end
    end

 end)

Please tell me what I am doing wrong.

Thanks

1 answer

Log in to vote
0
Answered by
0msh 333 Moderation Voter
4 years ago

because you're saving what's in player's backpack. this means when a player equip a tool, it is not in their backpack anymore. saving their startergear instead would be a better idea

0
I agree! DizzyGuy70 38 — 4y
0
So should I do on line 20 like this inside the loop "plr.Backpack, plr.StarterGear:GetChildren()"? Soban06 410 — 4y
0
just get the children of the StarterGear instead of Backpack 0msh 333 — 4y
0
Thanks. That worked. Soban06 410 — 4y
Ad

Answer this question