I have a coin purchase script, where it multiplies by the amount that the player currently has, I want to make it so it doesnt go down when the player spends their cash, instead I want it to keep the highest amount that it has been.
Module script inside ReplicatedStorage:
return function(initialAmount: number, fixedAmount: number, fixedThreshold: number, multiplierAmount: number): number
if initialAmount <= fixedThreshold then
return fixedAmount
else
return initialAmount * multiplierAmount
end
end
Display script on TextLabel:
local CashModule = game.ReplicatedStorage.CashModule
local calculateNewCashAmount = require(CashModule)
local fixedAmount = 200
local fixedThreshold = 199
local cashMultiplier = 1.5
local function Format(value)
local idp=1
if value < 1000 then return math.floor(value + 0.5)
else local abbreviations = {"", "K", "M", "B", "T"} local ex = math.floor(math.log(math.max(1, math.abs(value)),1000))
local abbrevs = abbreviations [1 + ex] or ("e+"..ex)
local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)
return ("%."..idp.."f%s"):format(normal, abbrevs)
end
end
local player = game.Players.LocalPlayer
player.leaderstats.Cash.Changed:Connect(function()
local amountToGive: number = calculateNewCashAmount(
player.leaderstats.Cash.Value,
fixedAmount,
fixedThreshold,
cashMultiplier
)
script.Parent.Text = tostring(Format(amountToGive))
end)
I’m assuming you’re using datastores to keep all of these values, right? From what I understand you want to have a product which once purchased gives the player a multiple of the most cash they had at once (for example they currently have 800k but they once had 1.4M, so the script would give them 5 times 1.4M), am I understanding correctly?
the script is made so each time the players cash changes it multiplies it by a certain amount, the more expensive the more it multiplies, im not using a datastore. if a player has got like 5k the lowest option would be like 7k and something, but the thing that I dont want it to do is go down in value once a player buys something with the cash. Like he could buy something with his 5k, lets say something for 4k, now the purchase amount given to him is like 1,5k not 7k.
yeah in that case set a variable for highest cash and just change it if the players cash is higer, like this::
local CashModule = game.ReplicatedStorage.CashModule
local calculateNewCashAmount = require(CashModule)
local fixedAmmount = 200
local fixedThreshold = 199
local cashMultiplier = 1.5
local highestCash = 0
local function Format(value)
local idp=1
if value < 1000 then return math.floor(value + 0.5)
else local abbreviations = {"", "K", "M", "B", "T"} local ex = math.floor(math.log(math.max(1, math.abs(value)),1000))
local abbrevs = abbreviations [1 + ex] or ("e+"..ex)
local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)
return ("%."..idp.."f%s"):format(normal, abbrevs)
end
end
local player = game.Players.LocalPlayer
player.leaderstats.Cash.Changed:Connect(function()
if player.leaderstats.Cash.Value > highestCash then
highestCash = player.leaderstats.Cash.Value
local amountToGive: number = calculateNewCashAmount(
highestCash,
fixedAmount,
fixedThreshold,
cashMultiplier
)
script.Parent.Text = tostring(Format(amountToGive))
end)
module:
return function(initialAmount: number, fixedAmount: number, fixedThreshold: number, multiplierAmount: number): number
if initialAmount <= fixedThreshold then
return fixedAmount
else
return initialAmount * multiplierAmount
end
end
Ok I forgot the most crucial part, it displays correctly like it doesnt go further down, but It doesnt give the purchased amount which is the most important part.
Script that handles purchases:
local MarketPlaceService = game:GetService("MarketplaceService")
local CashModule = game.ReplicatedStorage.CashModule
local players = game:GetService("Players")
local calculateNewCashAmount = require(CashModule)
local function onPurchase(receiptInfo)
local plr = players:GetPlayerByUserId(receiptInfo["PlayerId"])
local leaderstats = plr:FindFirstChild("leaderstats")
local cashToAdd = {
[1852179613] = calculateNewCashAmount(
leaderstats.Cash.Value,
200,
199,
1.5
),
[1852179828] = calculateNewCashAmount(
leaderstats.Cash.Value,
500,
499,
2
),
[1852179993] = calculateNewCashAmount(
leaderstats.Cash.Value,
2000,
1999,
2.5
),
[1852180091] = calculateNewCashAmount(
leaderstats.Cash.Value,
5000,
4999,
3
)
}
local cashAmount = cashToAdd[receiptInfo["ProductId"]]
leaderstats.Cash.Value += cashAmount
game.ReplicatedStorage.CashPurchaseEvent:FireClient(plr)
return Enum.ProductPurchaseDecision.PurchaseGranted
end
MarketPlaceService.ProcessReceipt = onPurchase