ProfileStore Inventory Issues

Hey all, I’ve been learning ProfileStore recently, as it seems like a good jumping off point for learning about Datastores. I’ve got the main profile saving system set up according to this YouTube tutorial.

I have two basic pieces of data: Cash, and the player’s inventory. The AddCash() function seems to work just fine, but it’s adding tools to the player’s inventory that just doesn’t work…

function Shared.AddCash(player: Player, amount: number)
	local data = Shared.GetData(player) -- GetData returns Profiles[profile].Data
	if not data then return end
	print("Data exists for "..player.Name)
	local cash = data.Cash
	
	data.Cash += amount
	print(`player has {cash} cash`)
	
	print(Shared.GetData(player))
end -- this works

I split the UpdateInventory() function into two parts, one that adds the item to the player’s profile Inventory, and one that adds the item to the player’s backpack:

function Shared.UpdateInventory(player: Player, item: string)
	local data = Shared.GetData(player)
	if not data then 
		warn("Data cannot be found for "..player.Name.." !")
		return
	end
	
	print("data exists")
	local inventory = data.Inventory
	table.insert(inventory, item)
	print("Inserted into inventory")
	
	print(Shared.GetData(player))
end

function Shared.AddToBackpack(player: Player, item: string)
	local data = Shared.GetData(player)
	if not data then 
		warn("Data cannot be found for "..player.Name.." !")
		return
	end
	
	print("data exists")
	
	local asset = ReplicatedStorage:WaitForChild("Assets"):FindFirstChild(item)
	if not asset then
		asset = ReplicatedStorage:WaitForChild("ModelAssets"):FindFirstChild(item)
	end
	print("Asset found.")
	
	if asset then
		asset.Parent = player.Backpack
	end
	
	print(Shared.GetData(player))
end

No matter which value I pass through this function, it always returns nil, and it’s driving me insane.

clickDetector.MouseClick:Connect(function(player)
	if not player then return end
	local item = script.Parent.Parent.Parent
	
	PlayerData.UpdateInventory(item.Name) -- somehow always ends up passing nil.
	print("added "..item.Name.." to inventory!")
	PlayerData.AddToBackpack(item.Name) -- same issue here.
	print("added "..item.Name.." to backpack!")
	
	item:Destroy()
end)

^^^
Example where I click on a tool and it adds the tool to the player’s inventory (in theory)
Screenshot 2025-07-26 082948

I really don’t know what I’m getting wrong here. If someone is able to help me figure out what I need to do to make a functioning inventory system here, please let me know! I’m new to DataStores in general and would really appreciate some assistance.

2 Likes

so looking through the code

in this function it calls for the player and the item name
function Shared.AddToBackpack(player: Player, item: string)

but you forgot to pass the player when calling the function
:cross_mark: PlayerData.UpdateInventory(item.Name)
:white_check_mark: PlayerData.UpdateInventory(player, item.Name)

and do the same with PlayerData.AddToBackpack(item.Name)

PlayerData.AddToBackpack(player, item.Name)

1 Like

omg that was really it :person_facepalming:
I wrote this at like 10:30PM last night. I think what happened was I somehow thought I was using RemoteEvents. Anway, rookie mistake I suppose. Thank you for your help!

1 Like

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