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

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

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

✓ Accepted answer

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

4 comments

0

I agree!

0

So should I do on line 20 like this inside the loop "plr.Backpack, plr.StarterGear:GetChildren()"?

0

just get the children of the StarterGear instead of Backpack

0

Thanks. That worked.

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