Tool save script not working

  1. I’m trying to make a save tool script for my game, this way users don’t have to buy the item multiple times.

  2. It doesn’t seem to save my tools when I leave and rejoin. Yesterday I was getting a queue issue, but it doesn’t show today but still, no tools save.
    Video: https://drive.google.com/file/d/1sdgyhNSsiKJZHSP8yUnJkb-FRQPS9UMI/view?usp=sharing

  3. No errors are printed in output so I haven’t tried much.

Script(note that this is a server sided script):

local DataStore = game:GetService("DataStoreService"):GetDataStore("ToolStore") --Get our data store

game.Players.PlayerAdded:Connect(function(plr)--When player is added fire this function 
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	
	local gold = Instance.new("IntValue")
	gold.Name = "gold"
	gold.Value = 5000
	gold.Parent = folder
	
	local data -- set our data var
	
	local success, errorMessage = pcall(function() --Make our pcall
		data = DataStore:SetAsync(plr.UserId)
	end)
	
	if data ~= nil then -- Data isn't nothing then
		for _, toolName in pairs(data) do  --Loop through its name
			local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName) -- Find our replicated storage folder
			if tool then
				local newTool = tool:Clone() -- Clone the tool
				newTool.Parent = plr.Parent -- Parent the tool to the player.
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player) --When the player is leaving do
	local toolsTable = {} --Set our tools table
	
	for _, tool in pairs(player.Backpack:GetChildren()) do --Loop through the players backpack
		if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then --If the tool is found in our replicated storage folder then
			table.insert(toolsTable,tool.Name) --Insert the tool name into the table
		end
	end
	local success, errorMessage = pcall(function() --Set our pcall
		DataStore:SetAsync(player.UserId, toolsTable)
	end)
end)

game:BindToClose(function() -- If the player is the last one in the server then
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through players
		local toolsTable = {} -- Make our tools table

		for _, tool in pairs(player.Backpack:GetChildren()) do -- Loop through players backpack
			if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then --If tool exists
				table.insert(toolsTable,tool.Name) --Insert tool into our table
			end
		end
		local success, errorMessage = pcall(function() --Set pcall
			DataStore:SetAsync(player.UserId, toolsTable)
		end)
	end
end)

You are not getting the data, actually saving. Replace it with GetAsync.

I set it to GetAsync and nothing happened, still no errors.

You are parenting the clone into the players service, not the Backpack folder. Maybe because of this, it wouldn’t save.

As you did here, they were no tools in the Backpack tool. So try changing the new parent of the tool.

I’ve saw this and updated it and yet it still didn’t work. Same results as before.

Edit: After printing it seems to save the tool successfully, but doesn’t clone the tool into the players backpack.

Are you getting this issue still? If so, you should have done it in the studio. That may be the problem. Toggle comment the PlayerRemoving function and try it again.

It saves perfectly(I tested it by using a comment) but doesn’t seem to clone the tool into the players backpack.

It got complicated so let’s check if the protected call fails to get the data.

if not success then
    print(errorMessage)
end

For every pcall.

Ight. Used my big brain and found out that Startergear exists and that solved my problem :sunglasses:

Thanks for your help anyway @ProBaturay!

1 Like