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.