Having problems with my sell platform

I am rewriting my code since I decided to go with ProfileService, and now my player’s inventories are stored in a table in their profile. I want to find the total amount of an item stored in the table profile, and multiply that by the amount it is worth, which is stored in a different table in a module in Replicated Storage.

I’m not getting any errors, but the players’ gold amount is not increasing, nor is the item amount being changed in their inventory.

Here is the script in the sell platform:

local sellPart = script.Parent
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local SystemsFolder = ServerStorage:WaitForChild("Systems")
local DataService = require(SystemsFolder.DataService)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ModulesFolder = ReplicatedStorage:WaitForChild("Modules")
local ItemData = require(ModulesFolder:WaitForChild("Harvestables"))

local function onTouch(playerTouched)
	if playerTouched.Parent:IsA("Model") and 
		playerTouched.Parent:FindFirstChild("Humanoid")
	then
		local player = Players:GetPlayerFromCharacter(playerTouched.Parent)
		if player then 
		local PlayerProfile = DataService:GetPlayerProfile(player)	
			local playerGold = PlayerProfile.Data.Gold
			local Inventory = PlayerProfile.Data.Inventory
			for items, amount in pairs(Inventory) do
				local Quantity = amount
				local Value = ItemData.Items[items].Worth
				local totalSell = Quantity * Value
				playerGold += totalSell
				items = 0
				PlayerProfile.Data.BagUsage = 0			
			end
		end	
		if not player then return end
	end
end
sellPart.Touched:Connect(onTouch)

Here is a snippet of the table in the module in Replicated Storage:

HarvestModule.Items = {
	Berries = {
		Name = "Berries",
		LayoutOrder = 1,
		Worth = 1,
	},

I’ve messed around with the code a few times, a previous error was attempt to multiply number with table, got that to go away but I’m still not seeing the results I would like. Am I overthinking this? Or do I need to rethink how I’m handling the inventory or the item information?

1 Like

you need to update the values in the profile data by setting those values back into the player’s profile data table.

for item, amount in pairs(Inventory) do
    local Quantity = amount
    local Value = ItemData.Items[item].Worth
    local totalSell = Quantity * Value
    playerGold += totalSell
    PlayerProfile.Data.Inventory[item] = 0
    PlayerProfile.Data.BagUsage = 0
end
PlayerProfile.Data.Gold = playerGold
PlayerProfile:Save() -- Save updated data into ProfileService

This should update the player’s gold and inventory data in their profile and save it back to ProfileService.

Thanks! I thought the code in another script was auto updating, but apparently not for this.

1 Like

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