Problem with converting TextBox.Text to a number

Hello again, I have a problem converting a string value to a number when using a TextBox. The output says that I’m trying to compare a string to a number on line 16. Do you know hoe to fix it? It looks right to me… Here is my script:

local Players = game.Players
local player = Players.LocalPlayer

local textBox = script.Parent

local maxSpeed = player.maxSpeed

textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code

local function OnFocusLost(enterPressed)
	if enterPressed then
		local speed = textBox.Text
		if tonumber(speed) then
			if speed <= 16 then -- Line 16
				player.Character.Humanoid.WalkSpeed = 16
			elseif speed >= 16 and speed <= maxSpeed.Value then
				player.Character.Humanoid.WalkSpeed = speed
			elseif speed >= maxSpeed.Value then
				player.Character.Humanoid.WalkSpeed = maxSpeed.Value
			end
		else
			textBox.Text = ""
			textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
		end
	else
		textBox.Text = ""
		textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
	end
end

textBox.FocusLost:Connect(OnFocusLost)

while true do
	textBox.PlaceholderText = "Type custom speed here (0-"..maxSpeed.Value..")"
	wait(1)
end

do this instead on line 15

  local speed = tonumber(textBox.Text)

remove line 16, you just checked for tonumber(speed) but compared the ‘speed’ variable which is the string textBox.Text with other numbers instead.

3 Likes