Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Knqe 23
5 years ago

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

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. mixgingengerina10 223 — 5y
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 Prestory 1395 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
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.

Ad
Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
I want each box to only contain one number, and it can't be 0. Knqe 23 — 5y

Answer this question