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

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)

Where do i put the New local script inside of? Im pretty sure Localscripts dont run inside of ServerScriptService

Oh yeah, put it in StarterGui or something like that. Sorry, I forgot to clarify that.

Im getting an error

Alright, should’ve researched that before-hand. Anyways, process the purchase and cash on the server, and use a bindable event to read the TextLabel’s texts.

Server Script:

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

function onPurchase(receiptInfo)
	-- your code here
	
	local cashToAdd = BindableEvent:InvokeClient()

    local cashAmount = cashToAdd[receiptInfo["ProductId"]]

    player.leaderstats.Cash.Value += cashAmount

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketPlaceService.ProcessReceipt = onPurchase

LocalScript:

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

function BindableEvent.OnClientInvoke(productId)
	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)
	}

    return cashToAdd
end

I went to cook myself some food, im back. I tried this and they both result in errors.

Sorry, my mistake. It was supposed to be BindableFunction, not a BindableEvent, hopefully it should work.