Can someone tell me what is wrong with my tool datastore?

Hi there, hope u r having a great day

Well, basically, im making a tool saving system so when player leaves doesnt lose his weapons. Howevar, for some reason, the script is not working altough no errors in output and/or squiggles in script.

The script is in ServerScriptService and the tools are in ServerStorage. I hope u can help me, thanks
: D

EDIT: not sure it has something to do with but the tools are obtained through a buy gui, which clones tool to backpack when bought

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("ArmaSave")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = dataStore:GetAsync("armas-"..player.UserId)
		if tool then
			for i, v in pairs(tool) do
				local toolFound = game.ServerStorage: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("armas-"..player.UserId,toolSave)
	end)
end)

There’s probably some errors but the pcalls are suppressing them. Try removing the pcalls and also printing the contents of the datastore for that specific key, basically adding a print(tool) after getting the datastore and storing it in tool and printing toolSave after the loop in playerremoving

1 Like

Are you testing this in Studio? If so, whenever you leave the game, the server shutsdown which doesn’t give the chance for PlayerRemoving to fire.

You also need to save data before the server shuts down:

game:BindToClose(function()
     for _, player in ipairs(game.Players:GetPlayers()) do
           coroutine.wrap(function()
              local toolSave = {}
	       	  for _, tool in ipairs(player.Backpack:GetChildren()) do
			     if tool then
				    table.insert(toolSave,tool.Name)
			   end
		   end
	   	  dataStore:SetAsync("armas-"..player.UserId,toolSave)
          end)()
     end
end)

The script looks good and shouldn’t error.

1 Like

tysm! the BindToClose function made script work! also removing the pcalls

Hi! Would you be able to rewrite this for a normal leaderstats datastore, instead of a tool datastore?
That would be SO HELPFUL!

There’s actually many tutorials for that, here’s a link for one: