Attempt to perform arithmetic (sub) on string and number. when the textbox is empty

In principle, the title already says what error is about. When the textbox is empty it gives this error. What to do? I will be grateful. Here is the script where this error occurs:

local ConvertFrame = script.Parent:WaitForChild("ConvertFrame")
local TextBoxConvert = ConvertFrame:WaitForChild("TextBoxConvert")
local FinallyValue = ConvertFrame:WaitForChild("FinallyValue")
local ButtonConvert = ConvertFrame:WaitForChild("ConvertButton")

local player = game.Players.LocalPlayer

local TimeStats = player.TimeFolder:WaitForChild("TimeValue")

local TimeChange = game.ReplicatedStorage:WaitForChild("TimeValueChange2")

local Amountt = 1

TextBoxConvert.Focused:Connect(function()
	
	TextBoxConvert:GetPropertyChangedSignal("Text"):Connect(function()
			
		FinallyValue.Text = TextBoxConvert.Text:gsub("%D+", "") - tonumber(Amountt)
		
	end)
	
end)

ButtonConvert.MouseButton1Click:Connect(function()
	
	local text = TextBoxConvert.Text
		
	TimeChange:FireServer(text)
	
end)

Mind telling on what line it is happening?

FinallyValue.Text = TextBoxConvert.Text:gsub("%D+", "") - tonumber(Amountt)
		

I’d say wrapping this around in an if statement

TextBoxConvert.Focused:Connect(function()
    TextBoxConvert:GetPropertyChangedSignal("Text"):Connect(function()
        local cleanedText = TextBoxConvert.Text:gsub("%D+", "")
        if cleanedText == "" then
            FinallyValue.Text = "Default Value" -- Set a default value when the textbox is empty
        else
            FinallyValue.Text = tostring(tonumber(cleanedText) - Amountt)
        end
    end)
end)
1 Like

I’ll check and answer right now.

you helped me out. Thank you very much. Good luck to you!

1 Like

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