Tonumber isn't properly updating, Cash isn't going to the player after a purchase

tonumber isnt updating, nor is it giving the player any money.
Screenshot:

Code:

local MarketPlaceService = game:GetService("MarketplaceService")

local players = game:GetService("Players")
--local BronzeId = 1852179613
--local SilverId = 1852179828
--local GoldId = 1852179993
-- DiamondId = 1852180091

function onPurchase(receiptInfo)
	
	local plr = players:GetPlayerByUserId(receiptInfo["PlayerId"])
	
	
	local leaderstats = plr:FindFirstChild("leaderstats")
	local playerGui = plr:FindFirstChild("PlayerGui")
	local playerCash = plr.leaderstats.Cash.Value
	
	local cashToAdd = {
		[1852179613] = tonumber(playerGui.CashShopGui.Frame.BronzeLabel.Text),
		[1852179828] = tonumber(playerGui.CashShopGui.Frame.SilverLabel.Text),
		[1852179993] = tonumber(playerGui.CashShopGui.Frame.GoldLabel.Text),
		[1852180091] = tonumber(playerGui.CashShopGui.Frame.DiamondLabel.Text)
	}
	
	local cashAmount = cashToAdd[receiptInfo["ProductId"]]
	
	playerCash += cashAmount
	print(cashToAdd)
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = onPurchase

Not sure why this issue is occurring, any help with this problem will be appreciated

What is the output if you do print(cashAmount)?

Why are you trying to access the PlayerGui from the server?

Is this a local script or server script? If you’re using a Gui so that the player can purchase you need to use a remote event so that a server script can preform the gamepass purchase and it can change the player’s leaderstats. Otherwise, you’ll only locally update the player’s cash.

1 Like

In the given screenshot, it prints out each fixed cash amount as the value, but when its over that amount, it still prints out the fixed amount

this is a server script in the serverscriptservice

Try this instead when defining and adding cash to playerCash:

local playerCash = plr.leaderstats.Cash

playerCash.Value += cashAmount

I think the issue is where you place .Value. I recommend to not put it in the variable, because it causes bugs like this. Just use .Value after defining the variable, as shown in the example code.

1 Like

it now gives the player the cash, BUT it still doesnt give cash once the value becomes bigger, let me try and show you



When you change the cash value, is it on the client or not?

not sure how to check, can u tell me what to do?

Here’s how to check whether it is changed on the client or not.

  • If you have any scripts that change the TextLabel’s text, check the class name of that script. If it is LocalScript, then it is on the client. If it is Script, then it’s on the server.
  • Do you change the TextLabel’s Text while playing the game in Roblox Studio? If yes, then check the upper right corner, where you should see Current: Client/Server. That says whether it is Server or Client. Else, it’s on the Server.

All of my Textlabels have a local script which changes the cash amount
image

heres the code (each one has different numbers but does the same thing)

local fixedAmount = 200
local cashMultiplier = 1.5
local player = game.Players.LocalPlayer

local function calculateNewCashAmount(player)
	local currentCash = player.leaderstats.Cash.Value 
	local newCashAmount = currentCash * cashMultiplier
	return newCashAmount
end

player.leaderstats.Cash.Changed:Connect(function()
	local player = game.Players.LocalPlayer
	local newCashAmount = calculateNewCashAmount(player)
	if player.leaderstats.Cash.Value <= 199 then
		script.Parent.Text = tostring(fixedAmount)
	else
		script.Parent.Text = tostring(newCashAmount)
	end
end)

I think I know the issue now. You are changing the value on the client, while you have the server process and give the cash. Unfortunately, that doesn’t work, because the client doesn’t replicate to the server. Try processing the purchase on the client, but through a remote event, give the cash on the server, so it isn’t exploitable. That way, it isn’t the server reading the text, rather the client.

 local cashValue = tonumber(playerGui.CashShopGui.Frame[label].Text)

Im most likely doing it wrong, i think i have an idea of what to do but im not sure how to implement it inside of my script

local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")
--local BronzeId = 1852179613
--local SilverId = 1852179828
--local GoldId = 1852179993
-- DiamondId = 1852180091

function onPurchase(receiptInfo)
	
	local plr = players:GetPlayerByUserId(receiptInfo["PlayerId"])
	
	
	local leaderstats = plr:FindFirstChild("leaderstats")
	local playerGui = plr:FindFirstChild("PlayerGui")
	local playerCash = plr.leaderstats.Cash
	
	local cashToAdd = {
		[1852179613] = tonumber(playerGui.CashShopGui.Frame.BronzeLabel.Text),
		[1852179828] = tonumber(playerGui.CashShopGui.Frame.SilverLabel.Text),
		[1852179993] = tonumber(playerGui.CashShopGui.Frame.GoldLabel.Text),
		[1852180091] = tonumber(playerGui.CashShopGui.Frame.DiamondLabel.Text)
	}
	
	local cashAmount = cashToAdd[receiptInfo["ProductId"]]
	
	playerCash.Value += cashAmount
	ReplicatedStorage.CashPurchaseEvent:FireClient(plr)
	
	print(cashToAdd)
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = onPurchase

Display (TextLabel)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local fixedAmount = 200
local cashMultiplier = 1.5
local player = game.Players.LocalPlayer

local function calculateNewCashAmount(player)
	local currentCash = player.leaderstats.Cash.Value 
	local newCashAmount = currentCash * cashMultiplier
	return newCashAmount
end

ReplicatedStorage.CashPurchaseEvent.OnClientEvent:Connect(function()
player.leaderstats.Cash.Changed:Connect(function()
	local player = game.Players.LocalPlayer
	local newCashAmount = calculateNewCashAmount(player)
	if player.leaderstats.Cash.Value <= 199 then
		script.Parent.Text = tostring(fixedAmount)
	else
		script.Parent.Text = tostring(newCashAmount)
	end
  end)
end)

I think you’ve got the idea but try flipping the order. Process the purchase and the TextLabel display in a LocalScript. Give the cash to the player in a server script, though. I can show an example if it helps.

Please show me an example, id like to know

Alright, here’s an example of what it would look like:

Server Script:

local RemoteEvent = game:GetService("ReplicatedStorage").CashPurchaseEvent

RemoteEvent.OnServerEvent:Connect(function(Player, CashAmount)
	Player.leaderstats.Cash.Value += CashAmount
end)

LocalScript:

function onPurchase(receiptInfo)
	-- your code here
	
	ReplicatedStorage.CashPurchaseEvent:FireClient(cashAmount)

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = onPurchase

(I haven’t tested this, so please tell me if it doesn’t work/breaks.)

With the localscript, do i put ur lines of code inside of the Display script?

No, don’t put it in the display script. Just make a new LocalScript, or replace the old script with a LocalScript. I actually don’t think it is necessary to change the display script at all, so you can revert it back to the original, where there weren’t any changes. (Check Post #13 if it helps to find the original)