Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

attempt to perform arithmetic on a nil value? What does this mean?

Asked by 5 years ago
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

2 answers

Log in to vote
1
Answered by 5 years ago

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)
1
Thank you I did't know what Avigant mean't but now I know since I saw the script. I guess next time for future people who need help just use the tonumber in the way your firing the event Sergiomontani10 236 — 5y
Ad
Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago

Player.stats.Bank.Value or Player.stats.Money.Value (or both) is nil, likely meaning that you created ObjectValues instead of IntValues or NumberValues. 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.

0
Yup it is not nill and is correctly typed it is using a IntValue Sergiomontani10 236 — 5y
0
Oh I'm sorry, I don't know what I was thinking. Clearly, HowMuch is nil. Avigant 2374 — 5y
0
It is saying that HowMuch is a nil value Sergiomontani10 236 — 5y
0
Exactly, that's your problem. You're attempting to convert it with tonumber(HowMuch), which will return nil. How are you firing the Remote? You also shouldn't assume that HowMuch exists and is a number, or trust it. Avigant 2374 — 5y

Answer this question