Error: attempt to perform arithmetic (add) on number and nil

I’m working on a bank deposit/withdrawal system but I came across this error that’s been intriguing me for a while.

Basically, you type the value you want to deposit on a textbox and then you press Confirm and the money should be deposited into your account. Although I’m getting the following error:

ServerScriptService.Dinheiro:45: attempt to perform arithmetic (add) on number and nil

This is what my code in the button looks like (LocalScript):

local notification = require(game.ReplicatedStorage.Notifications)
local replicated = game:GetService("ReplicatedStorage")
local event = replicated:FindFirstChild("Multibanco"):WaitForChild("Depositar")
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Dinheiro.Value > 0 then
		event:FireServer(script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Name, script.Parent.Dinheiro.Value)
		notification.Notify("Success","Você depositou " .. script.Parent.Dinheiro.Value .. "€ com sucesso!",5)
	else
		notification.Notify("Error","O valor a depositar deve ser um número maior que 0.",5)
	end
end)

This is what the ServerScriptService script looks like:

lua
game.ReplicatedStorage.Multibanco.Depositar.OnServerEvent:Connect(function(player, amount)
	if player:FindFirstChild("DinheiroBanco")  then
		local bank = player.DinheiroBanco
		local money = player.Dinheiro
		bank.Value = bank.Value + amount
		money.Value = money.Value - amount
	end
end)

Any help is appreciated!

You can try in the server:

game.ReplicatedStorage.Multibanco.Depositar.OnServerEvent:Connect(function(ply, player, amount)
	if player:FindFirstChild("DinheiroBanco")  then
		local bank = player.DinheiroBanco
		local money = player.Dinheiro
		bank.Value = bank.Value + amount
		money.Value = money.Value - amount
	end
end)

in the event arguments we put first the player, then the arguments. (in there the player is repeated)

the FireServer() event on the server is returned 3 values: Player, script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Name, and script.Dinherio.Value.

On the server, the first argument by default given is the Player who fired it, then the next arguments after that are the ones you sent. So in this case amount is actually script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Name.

This is the error now, ServerScriptService.Dinheiro:42: attempt to call missing method ‘FindFirstChild’ of string

So, I only need to have amount?

check if the player on the argument is the name or the instance, if is the name do local playerinplayers = game:GetService("Players"):FindFirstChild(player)

Im going to assume that script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Name not the money value, and if so, try this:

game.ReplicatedStorage.Multibanco.Depositar.OnServerEvent:Connect(function(player, PlayerName , amount) -- PlayerName is not necessary because by default its sent to the server automatically (the server needs to know the client that sent the request to take the request)
	if player:FindFirstChild("DinheiroBanco")  then
		local bank = player.DinheiroBanco
		local money = player.Dinheiro
		bank.Value = bank.Value + amount
		money.Value = money.Value - amount
	end
end)

Replace the player variable in player:FindFirstChild("DinheiroBanco") and player.DinheiroBanco lastly player.Dinheiro with the ply variable

Basically, I changed the :FireServer() in the LocalScript to the money value only. Thank you for explaining!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.