Leaderstats values not taking other scripts into consideration when deducting a value

I have two separate server scripts which both contain code to deduct a Leaderstats value named “Cash”; One deducts in increments of 50, while the other is far more varied (it’s in regards to item costs).

The problem, as stated in the title of this post, is that when a player buys an item after the 50-increment deduction, the system uses the ‘Cash’ Leaderstats value from before that deduction.

To better illustrate the problem, suppose a player has 500 cash. If they perform an action that removes 50 cash from their Leaderstats value, resulting in 450 cash, and then immediately buy an item costing 15 cash, the system disregards the initial 50-cash deduction and removes 15 cash from the original 500.

I’ve tried various methods to fix the problem. For instance, instead of instantiating the ‘Cash’ variable in the server scripts like this:

local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
	local cash = leaderstats:FindFirstChild("Cash")
	if cash then
		cash.Value -= incrementCost
	end
end (server script 1)

local leaderstats = player:FindFirstChild("leaderstats")
local playerCash = leaderstats:FindFirstChild("Cash")
if playerCash.Value >= itemPrice then
	playerCash.Value -= itemPrice
end (server script 2)

I had changed it to instance the variables more directly like this (from looking at some of the other developer topics around the Leaderstats):

player.leaderstats.Cash.Value -= incrementCost (server script 1)

player.leaderstats.Cash.Value -= itemPrice (server script 2)

Despite these and many other tweaks to the code, neither ServerScript seems to respect the changes the other one does to the Leaderstats value. I am not sure what exactly I am doing wrong, but I hope that the script snippets I provided would be a good starting point in diagnosing the issue.

Note: The script snippets have been modified in their variable names to be more generic and seamless in case a solution is found that developers here can also use.

Why don’t you just keep track of each player’s cash in a value or table then display those values through the leaderstats when it changes.

2 Likes

Can you provide a place file so I can take a look closer?

I don’t think this is an issue with values themselves, that kind of race condition is impossible in serial execution.

1 Like

Are you sure they are both server scripts?
Also let’s say the player buys something from script one, does the actual leaderstat in tab go to 450? And then let’s say in script 2 they buy something for 30, does it jump back to 470?

I agree with the first response, if the problem persists, store your own cash value that is just sent to the leaderstat as almost a UI container rather than the actual host of the real data. It’s typically safer too. Also, if you are looking to save your cash through DataStores, then this would also help.

1 Like

So in my attempt at creating a new place file to showcase the error, I may have ended up fixing issue by diagnosing it as a Client and Server inconsistency. Mainly the Increment deduction doesn’t deduct on the server, while the tool buying does.

I will edit this post with the fix later on as I try to troubleshoot the issue. In the meantime I might as well provide the isolated place file:
leaderstatsCashConsistency.rbxl (69.9 KB)

@Veintore To answer your question, yes, the scenario you brought up does indeed have this issue.