Here’s a quick, clean fix plus a couple of safety checks so players can’t break the economy:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Remotes
local CashToBank = ReplicatedStorage:WaitForChild("CashtoBank")
local BankToCash = ReplicatedStorage:WaitForChild("BanktoCash")
-- Helper: make sure the amount is a positive whole number
local function isPositiveInteger(value)
return typeof(value) == "number" and value > 0 and value % 1 == 0
end
--------------------------------------------------------------------
-- Cash ➜ Bank
--------------------------------------------------------------------
CashToBank.OnServerEvent:Connect(function(player, amount)
-- Validate the payload!
if not isPositiveInteger(amount) then return end
local leaderstats = player:WaitForChild("leaderstats")
local cash = leaderstats:WaitForChild("Cash")
local bank = leaderstats:WaitForChild("Bank")
-- Don’t let them deposit more than they have
if amount > cash.Value then return end
cash.Value -= amount -- Luau supports += / -=
bank.Value += amount
end)
--------------------------------------------------------------------
-- Bank ➜ Cash (optional, in case you need it)
--------------------------------------------------------------------
BankToCash.OnServerEvent:Connect(function(player, amount)
if not isPositiveInteger(amount) then return end
local leaderstats = player:WaitForChild("leaderstats")
local cash = leaderstats:WaitForChild("Cash")
local bank = leaderstats:WaitForChild("Bank")
if amount > bank.Value then return end
bank.Value -= amount
cash.Value += amount
end)
What was wrong with the original snippet?
cashamount(string:match("%W"))
– cashamount is a variable, not a function. You probably wanted to validate the string instead.
- You never actually changed the player’s money:
– Value + cashamount just calculates a number and throws it away. You must assign (Value = …) or use +=.
- No sanity checks:
– Exploiters could send negative or huge values.
The revised script:
• Checks that the amount is a positive integer.
• Makes sure the player has enough money before transferring.
• Uses Luau’s += / -= or plain Value = Value + amount if you prefer.