Save Inventory Issue

I want to create a SaveInventory Script that saves your tools when leaving and rejoining. While it works the only issue I want to solve is that when the player resets the tool is still there. I want the tool to load only when you join the game. Otherwise, if you reset your character you should lose the tool.

local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
	local joined = Instance.new("IntValue")
	joined.Parent = plr
	joined.Name = "ToolSave"
	joined.Value = 1
	task.wait(2)
	if joined.Value == 1 then
		joined.Value = 0
		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.Weapons:FindFirstChild(v)
					if tool then
						tool:Clone().Parent = plr:WaitForChild("Backpack")
						tool:Clone().Parent = plr:WaitForChild("StarterGear")
					end
				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)

Do you have API service enabled in studio?

tool:Clone().Parent = plr:WaitForChild("StarterGear")
There’s your smoking gun.

StarterGear is what the game instances into your backpack everytime your character loads, so putting an item in there does the give-on-every-reset effect.

Yes, like I said above it works, but when I reset the tools are still there. I want the tools to load only when the player joins the game and I have tried to do that and it doesn’t seem to work.

Oh, I think that’s the problem.

Yes, that was the problem solved.

The tool is not working as expected when I join the game and it loads.

This sounds more like a problem with the tool itself

Seems like it was a problem with the character. However, when I have the tool Equipped it doesn’t save.

Here’s a cool quirk about Roblox; equipped tools are not in your Backpack, but your character. You’re only checking for the Backpack, and so…

So, where are they is there any ways to solve this?

I saw it inside the player’s character.

You can check the character to see if it has a tool using something such as plr.Character:FindFirstChildOfClass("Tool")

So, I made it like that:

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)
	elseif plr.Character:FindFirstChildOfClass("Tool") then
	local EquippedTool = plr.Character:FindFirstChildOfClass("Tool")
	table.insert(toolsToSave, EquippedTool.Name)
   
   end
  end
  ds:SetAsync(key,toolsToSave)
 end)
end)

This will give an error if you don’t have a tool equipped. Just check if EquippedTool is nil

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.