game.ReplicatedStorage.Money.ATM.Withdraw.OnServerEvent:Connect(function(Player, HowMuch) Player.stats.Bank.Value = Player.stats.Bank.Value - tonumber(HowMuch) Player.stats.Money.Value = Player.stats.Money.Value + tonumber(HowMuch) end)
Seems like a simple problem but I don't see how it isn't working. The error I get is this.
ServerScriptService.MoneySystemEvents2:16: attempt to perform arithmetic on a nil value
You could have a GUI that pops up when the ATM is clicked, and with a button that lets you withdraw money, with a TextBox
to put an amount. I will remake the code, as well as the Gui coding part. I also added another RemoteEvent
that fires to the client if they try to get more money than what they have.
--Server Script. Here we make sure the player has enough bank money, to withdraw. local withdraw = game:GetService("ReplicatedStorage").Money.ATM.Withdraw withdraw.OnServerEvent:Connect(function(plr, amount) if plr.stats.Bank.Value >= amount and plr.stats.Bank.Value > 0 then --if they have more than or the exact amount of money they want to withdraw plr.stats.Money.Value = plr.stats.Money.Value + amount else withdraw.Parent.WithdrawError:FireClient(plr, "Not enough money!") -- there could be a fail transaction. you can fake failed ones just for fun or if something actually happens custom error message end end)
The LocalScript
with the TextBox
to insert amount.
--Local Script local withdraw = game:GetService("ReplicatedStorage").Money.ATM.Withdraw local function makeTextNumber() tonumber(script.Parent.Parent.MoneyAmount.Text) end pcall(makeTextNumber) script.Parent.MouseButton1Click:Connect(function() withdraw:FireServer( tonumber(script.Parent.Parent.MoneyAmount.Text) -- wrapped in a pcall, made tonumber, it should all be good ) makeTextNumber() end)
Now for the WithdrawError
RemoteEvent
I made in the Server script.
-- Local Script local withdrawError = game:GetService("ReplicatedStorage").Money.ATM.WithdrawError withdrawError.OnClientEvent:Connect(function(msg) script.Parent.Text = msg wait(2) script.Parent.Text = "Withdraw" --whatever the default text is put here end)
Player.stats.Bank.Value
or Player.stats.Money.Value
(or both) is nil
, likely meaning that you created ObjectValue
s instead of IntValue
s or NumberValue
s. Additionally, you shouldn't assume that Player.stats
exists, and you should first check if they have enough money to withdraw before granting it. You should also check that HowMuch
is not a negative number, because they'd be able to subtract a negative from their bank value.