Attempt to perform arithmetic (add) on number and string

I am trying to make a refund option in my mod panel. Here is my code:

local Button = script.Parent.Parent.Submit.TextButton
local Amount = script.Parent.Parent.Parent.Refund.Amount.TextBox.Text

Button.MouseButton1Click:connect(function()
	local Player = script.Parent.Parent.Username.TextBox.Text
	local PlayerFound = game.Players:FindFirstChild(Player)
	
	if PlayerFound then
		game.Players:FindFirstChild(Player).leaderstats.Points.Value = game.Players:FindFirstChild(Player).leaderstats.Points.Value + Amount
		game.Players:FindFirstChild(Player).leaderstats.Wins.Value = game.Players:FindFirstChild(Player).leaderstats.Wins.Value + Amount
		
		Amount = "Amount"
		Player = "Username"
    end
end)

However, when I click submit, I am getting this error:

attempt to perform arithmetic (add) on number and string

I would appreciate it if someone could please help me out. Thanks. :slightly_smiling_face:

2 Likes

It’s saying that the Amount variable is a string value and not a number value.

Amount is a number value, though.

Did you put quotations around it when defining it?

No, as seen in the script above.

You should define amount without putting .Text and when adding the amount add .Text.

1 Like

How do you know it’s double?
Trying printing type(value) to both of these value see what does it return? as proven by the error message, you’re adding double to string.

Btw, unless you’re on vanilla Lua, you can do (this is a new feature)

game.Players:FindFirstChild(Player).leaderstats.Points.Value += Amount

instead of

game.Players:FindFirstChild(Player).leaderstats.Points.Value = game.Players:FindFirstChild(Player).leaderstats.Points.Value + Amount

try use

tonumber(Amount)

Making the text in the textbox a number instead of a string

2 Likes

When I use your method, I get no errors but nothing happens.

local Button = script.Parent.Parent.Submit.TextButton
local Amount = script.Parent.Parent.Parent.Refund.Amount.TextBox.Text

Button.MouseButton1Click:Connect(function()
    local player = script.Parent.Parent.Username.TextBox.Text
    local PlayerFound = game.Players:FindFirstChild(player)
    if PlayerFound and type(tonumber(Amount)) == "number" then
          game.Players:FindFirstChild(Player).leaderstats.Points.Value = 
        game.Players:FindFirstChild(Player).leaderstats.Points.Value + tonumber(Amount)
	 game.Players:FindFirstChild(Player).leaderstats.Wins.Value = 
        game.Players:FindFirstChild(Player).leaderstats.Wins.Value + tonumber(Amount)
    end
end)

also why are you setting it to a string

When I use your method, it still throws the same error.

Your method doesn’t work.

Are you sure you are doing it right? Why are they in a different line?

Change Amount to

local Amount = tonumber(script.Parent.Parent.Parent.Refund.Amount.TextBox.Text)

1 Like

I am also getting two other errors. Not sure if they are related to this script or not:

Attempt to connect failed: Passed value is not a function

and

attempt to call a nil value

Did you even check the type of the Amount and the game.Players:FindFirstChild(Player).leaderstats.Wins.Value (or whatever you’re adding)?
You have exemplified the importance of type checking.

Your method didn’t work. Nothing happens but I get still get the same error I started with.

1 Like

What does amount equal then usually? This is sort of vague without any knowledge of what you’re trying to accomplish.

I am making a refund option in my mod panel to refund players currency.

That does not answer my question. What will amount equal?

Also, this should NOT be on the client entirely, I can assure you you will run into problems after you fix this error. The value should 10)% of the time be changed on the server side.