Data script doesn't load tools

Hello! My datastore script isn’t loading tools:
Data handler script:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Data")

local function GenerateUID(player)
	return "UID_"..player.UserId
end

function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local tokens = Instance.new("IntValue")
	tokens.Name = "Tokens"
	tokens.Parent = leaderstats
	
	
	local data
	local key = GenerateUID(player)
	
	local success, err = pcall(function()
		data = DS:GetAsync(key)
	end)
	if not success then
		player:Kick("\nDataHandler\n\nCouldn't fetch data!")
	else
		for i,v in pairs(data.Tools) do
			print(v)
			if game.ReplicatedStorage.Tools:FindFirstChild(v) then
				local clone = game.ReplicatedStorage.Tools[v]:Clone()
				clone.Parent = player.Backpack
				clone.Parent = player.StarterGear
			end
		end
	end
end

function onPlayerRemoving(player)
	local data = {
		Tokens = player.leaderstats.Tokens.Value,
		Tools = {
			--starter sword
		}
	}
	local key = GenerateUID(player)

	local success, err = pcall(function()
		data = DS:SetAsync(key, data)
	end)
	if not success then
		warn("Error while saving data: "..err)
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)

Remote handler script:

local Remotes = game:GetService("ReplicatedStorage").Remotes

Remotes.Shop.OnServerEvent:Connect(function(player, tool)
	local data = game:GetService("DataStoreService"):GetDataStore("Data"):GetAsync("UID_"..player.UserId)
	print(data)
	local s, e = pcall(function()
		table.insert(data.Tools, tool)
	end)
end)

Localscript that fires the shop remote:

local player = game.Players.LocalPlayer

repeat wait() until player

local tools = game.ReplicatedStorage.Tools
local tokens = player.leaderstats.Tokens
local shop = player.PlayerGui.Shop
local shopre = game.ReplicatedStorage.Remotes.Shop

local debounce = false

    for i, v in pairs(script.Parent:GetChildren()) do
    	if not debounce then
    		debounce = true
    		if v:IsA("Frame") then
    			local button = v.Image
    			button.MouseButton1Click:Connect(function()
    				local price = v:GetAttribute("Price")
    				if not player.Backpack:FindFirstChild(v) then
    				if tokens.Value >= price then
    					if tools[v.Name] then
    						tools[v.Name]:Clone().Parent = player.Backpack
    							tools[v.Name]:Clone().Parent = player.StarterGear
    							shopre:FireServer(v.Name)
    						shop.Enabled = false
    					end
    				else
    					print(player.Name..' doesn\'t have enough for '..v.Name)
    					shop.Enabled = false
    				end
    				else
    					print(player.Name..' already has '..v.Name)
    					shop.Enabled = false
    				end
    			end)
    		end
    	end
    	task.wait(1)
    	debounce = false
    end

You’re immediately removing it from the player’s backpack

No it doesn’t?
image

Is the tool not showing up in the actual game?

No, I assume there’s some error while saving/loading.

Okay, just to be clear: You’re seeing the tool in the explorer and in-game, but your tools either don’t save or load when rejoining?

No, I have a shop system. That’s where I’m getting the sword.

I’m confused as to what your issue is. Just reply with the number that represents your situation, and add a little description afterwards so you get over the word limit.

  1. You don’t load in with the item even though it shows it in the explorer

  2. The item is being purchased from the shop correctly, but when you rejoin, it’s no longer there.

  3. Your issue isn’t listed.

That was after I bought it in the shop.

Yes

I’m confused how it isn’t listed

I’m giving you scenarios so I can better understand what your issue is. It’s not very clear in your post. How do you know your script isn’t loading tools? Where are you seeing what’s being loaded? After which point is it supposed to be saved? What are your errors? What have you already tried to fix it?

You should include all of this and more in your initial post.

The error is from a plugin, don’t worry about that.

Is Studio’s access to API services enabled? It should be in your game settings.

Yes it is, and I’m not getting any errors.

Okay, two things:

Remove the data =. I don’t think it should make a difference, but best to do it anyway.

You’re setting a completely new Tools table that’s empty. When you save this, it saves that empty table that you’ve initialized.

I’m still looking at the tokens issue. Let me know if there’s any change with the tools.

Your tokens not saving may be a separate issue. Are you changing this value on the server or the client? Can you post the script?

Sorry, I went to go eat.

It’s a server script in serverscriptservice. Here’s the code.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Data")

local function GenerateUID(player)
	return "UID_"..player.UserId
end

function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local tokens = Instance.new("IntValue")
	tokens.Name = "Tokens"
	tokens.Parent = leaderstats
	
	
	local data
	local key = GenerateUID(player)
	
	local success, err = pcall(function()
		data = DS:GetAsync(key)
	end)
	if not success then
		player:Kick("\nDataHandler\n\nCouldn't fetch data!")
	else
		print("Successfully got data")
		for i,v in pairs(data.Tools) do
			print(v)
			if game.ReplicatedStorage.Tools:FindFirstChild(v) then
				local clone = game.ReplicatedStorage.Tools[v]:Clone()
				clone.Parent = player.Backpack
				clone.Parent = player.StarterGear
			end
		end
	end
end

function onPlayerRemoving(player)
	local data = {
		Tokens = player.leaderstats.Tokens.Value,
		Tools = {
			--starter sword
		}
	}
	local key = GenerateUID(player)

	local success, err = pcall(function()
		DS:SetAsync(key, data)
	end)
	if not success then
		warn("Error while saving data: "..err)
	else
		print("Successfully saved data")
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)

Then how do I save the table instead of resetting it?

I fixed the tokens not loading. Just had to add this line.

tokens.Value = data.Tokens

You need to either store some sort of global or shared table, or just Instance:GetChildren() the player’s folder of tools. You definitely should not use the actual tools that the player will be using as the reference, though. You could save the table every time the player makes a purchase.

Anything along those lines.

Here’s my new data script, still not loading.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Data")

local function GenerateUID(player)
	return "UID_"..player.UserId
end

function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local tokens = Instance.new("IntValue")
	tokens.Name = "Tokens"
	tokens.Parent = leaderstats
	
	local folder = Instance.new("Folder")
	folder.Name = player.Name
	folder.Parent = game.ReplicatedStorage.PlayerTools
	
	local data
	local key = GenerateUID(player)
	
	local success, err = pcall(function()
		data = DS:GetAsync(key)
	end)
	if not success then
		player:Kick("\nDataHandler\n\nCouldn't fetch data!")
	else
		tokens.Value = data.Tokens
		print("Successfully got data")
		for i,v in pairs(data.Tools) do
			print(v)
			if game.ReplicatedStorage.Tools:FindFirstChild(v) then
				local clone = game.ReplicatedStorage.Tools[v]:Clone()
				clone.Parent = player.Backpack
				clone.Parent = player.StarterGear
			end
		end
	end
end

function onPlayerRemoving(player)
	local data = {
		Tokens = player.leaderstats.Tokens.Value,
		Tools = game.ReplicatedStorage.PlayerTools[player.Name]:GetChildren()
	}
	print(data.Tools)
	local key = GenerateUID(player)

	local success, err = pcall(function()
		DS:SetAsync(key, data)
	end)
	if not success then
		warn("Error while saving data: "..err)
	else
		print("Successfully saved data")
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)