Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make this text box only accept numbers as it's text?

It's suppose to make a test equal to nothing if a player inserts letters etc.

while true do
	if script.Parent.Text == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 0 or " " then
	else
		script.Parent.Text = " "
	end
end

https://gyazo.com/90eef064a84956a9a0b97db79e700023

2 comments

0

or is not written like this, it may sound right, but its not how lua works, it's supposed to be if script.Parent.Text == 1 or script.Parent.Text == 2 or script.Parent.Text == 3.. etc, plus there is nothing after your then.

2

Use tonumber if it returns nil then wipe the text else just  return and instead of using a while loop use GetPropertyChangedSignal on the TextBox's Text

2 answers

✓ Accepted answer
local textbox = script.Parent
local function onFocusLost (enterpressed)
	if enterpressed then -- checks if the user has entered a number
		if textbox.Text == "1" or textbox.Text == "2" or textbox.Text == "3" or textbox.Text == "4" or textbox.Text == "5" or  textbox.Text == "6" or textbox.Text == "7" or textbox.Text == "8" or textbox.Text == "9" then
			print("Player entered a valid number")
		else
			print("Player did not enter a valid number")
		end
	end
end
textbox.FocusLost:Connect(onFocusLost) -- this event let's the function run when the client loses their focus of the TextBox.

So, i made the numbers strings, because the text of a textbox is always a string.

and i added the FocusLost event so that it would only run when the client has entered text in the textbox.

So what you want to do is use tonumber on the text box's text if it does not return nil that means the text box contains numbers but if it does return nil that means the text box contains another character which is not a number.

Another thing to note instead of using a while loop is use :GetPropertyChangedSignal on the text box's text which would fire this function every time the text changes.

function NumberValidation ()
	if tonumber(script.Parent.Text) ~= nil then
		return
		else script.Parent.Text = ""
	end
end

script.Parent:GetPropertyChangedSignal("Text"):Connect(NumberValidation)

1 comment

0

I want each box to only contain one number, and it can't be 0.

This discussion is preserved as it was. New answers, comments, and votes are closed.