Detecting if a player typed a number that is 1-100

Hey!

I need help solving this issue I am having or really just support on how to execute this idea that I have.

I want to use a text box as some sort of volume changer for a sound I have in the game and I need to detect if the player typed in a number that is 1-100, let me show you my script.

local number = {1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}

local box = script.Parent:WaitForChild("TextBox")
local submit = script.Parent:WaitForChild("TextButton")

submit.MouseButton1Click:Connect(function()
   if box.Text == #number then
		print(box.Text)
   else
      print("You have to type a number.")
   end
end)

In this script I’m basically using a table to try to detect if a user imputed one of the numbers in that table into the text box, if anyone could help me on this please let me know.

Thank you!

#operator means “length of array”

you want to see if a player typed a number, you don’t need an array with numbers in it for that.

You can do:

submit.MouseButton1Click:Connect(function()
    return math.clamp(tonumber(box.Text),0,100) -- this will clamp the number they typed from 0-100
end)
1 Like

Thanks this really helped!

I just wanted to ask how to remove the error it gives me when I type something that isn’t a number 1 - 100, is there a way to make it so if you type anything else it does return end?

You should only allow them to type numbers, you can do this by filtering the text as they type it

box:GetPropertyChangedSignal("Text"):Connect(function()
	box.Text = box.Text:gsub('%D+', '')
end)

You can learn more about string manipulation on these pages

You can even add to that and not let them type numbers below 0 or above 100

box:GetPropertyChangedSignal("Text"):Connect(function()
	box.Text = box.Text:gsub('%D+', '')
    box.Text = tostring(math.clamp(tonumber(box.Text),0,100))
end)
1 Like

Here fixed the code so no errors

box:GetPropertyChangedSignal("Text"):Connect(function()
	box.Text = box.Text:gsub('%D+', '')
	local number = tonumber(box.Text)
	box.Text = number ~= nil and tostring(math.clamp(number,0,100)) or ""
end)
1 Like
local frame = script.Parent
local box = frame:WaitForChild("TextBox")
local submit = frame:WaitForChild("TextButton")

submit.MouseButton1Click:Connect(function()
	if tonumber(box.Text) then
		if type(tonumber(box.text)) == "number" then
			box.Text = math.round(tonumber(box.Text)) --remove any possible decimals
			if tonumber(box.Text) >= 1 and tonumber(box.Text) <= 100 then
				print("You typed the number "..box.Text.." into the textbox!")
			else
				print("The number must be between 1 and 100!")
			end
		else
			print("You must type a valid number!")
		end
	else
		print("You must type a valid number!")
	end
end)

Don’t kick yourself after seeing this. I tried to stay true to the original source material.