[Solved] Tools save but don't load

I have this script and when the player leaves it saves the tools but when he joins they don’t load. Here is the script.

local PlayerDataHandler = {}
local dataTemplate = {
	Inventory = {}
}

local ProfileService = require(game.ServerScriptService.ProfileService)
local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerProfile", 
	dataTemplate)

local Profiles = {}

local function playerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)
		if not player:IsDescendantOf(Players) then
			profile:Release()
		else
			Profiles[player] = profile
		end
		print(dataTemplate)
		
		player.CharacterAdded:Connect(function()
			
			loadBackpack(player)
		end)
	else
		player:Kick()
		
	end
end

function loadBackpack(player)

	local inventory = PlayerDataHandler:Get(player, "Inventory")
	
	for _, item in ipairs(inventory) do
		
		local asset = game.ServerStorage.Weapons:FindFirstChild(item)
        print(asset.Name)

		if asset then
			local Newasset = asset:Clone()
			Newasset.Parent = player.Backpack
			print(Newasset.Parent.Parent)
			print(player.Backpack)
		end
		
		
	   
	end
	table.clear(inventory)
end
	


function PlayerDataHandler:Init()
	for _, player in ipairs(Players:GetPlayers()) do
		task.spawn(playerAdded, player)
	end
	Players.PlayerAdded:Connect(playerAdded)
	Players.PlayerRemoving:Connect(function(player)
		
		if Profiles[player] then
			PlayerDataHandler:SaveInventory(player)
			Profiles[player]:Release()
		end
	end)
end

local function getProfile(player) 
	assert(Profiles[player], string.format("Profile does not exist for %s", player.UserId))
	return Profiles[player]
end

--Getter-Setter Methods
function PlayerDataHandler:Get(player, key)
	local profile = getProfile(player)
	assert(profile.Data[key], string.format("Data does exist for key %s", key))
	
	return profile.Data[key]
end

function PlayerDataHandler:Set(player, key, value)
	local profile = getProfile(player)
	assert(profile.Data[key], string.format("Data does exist for key %s", key))
	
	assert(type(profile.Data[key]) == type(value))
	
	profile.Data[key] = value
end

function PlayerDataHandler:SaveInventory(player)
	

	for i, v in ipairs(player.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			
			print(v.Name)
			table.insert(dataTemplate.Inventory, v.Name)
		end
	end
	self:Update(player, "Inventory")

	
end


function PlayerDataHandler:AddToInventory(player, itemName)
	self:Update(player, "Inventory", function(current)
		table.insert(current, itemName)
		
		
		return current
	end)
	
	local item = game.ServerStorage:FindFirstDescendant(itemName)
	if not item then return end
	item:Clone().Parent = player.Backpack
end

function PlayerDataHandler:RemoveFromInventory(player, itemName)
	self:Update(player, "Inventory", function(current)
		if table.find(current, itemName) then
			table.remove(current, table.find(current, itemName))
		end
		return current
	end)
		if player.Backpack:FindFirstChild(itemName) then
			player.Backpack[itemName]:Destroy()
		elseif player.Character:FindFirstChild(itemName) then
			player.Character[itemName]:Destroy()
		end
end

function PlayerDataHandler:Update(player, key, callback)
local profile = getProfile(player)	
local oldData = self:Get(player, key)
local newData = callback(oldData)

self:Set(player, key, newData)

end



return PlayerDataHandler
1 Like

I was able to fix it seems like a line was in the wrong position.

1 Like

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