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

How do I make a TextBox only accept numbers?

Asked by 9 years ago

So I'm making a Light Placing plugin (I know, super original!) and when i click, I want a gui to appear asking you things like "How bright?". But, obviously, is someone typed letters in it, it would obviously error.

0
Have the script check if tonumber(string) ~= nil. tonumber will return nil if the string given is not a number at all. M39a9am3R 3210 — 9y

1 answer

Log in to vote
2
Answered by
theCJarmy7 1293 Moderation Voter
9 years ago

M39a9am3R is right.

1--tonumber will turn a string into a number or return nil if the string is not a number
2tonumber("hello!")--nil
3tonumber("1244")-1244

Then to have the TextBox only use numbers, just do something like this.

1textbox = script.Parent.TextBox --or something
2textbox.FocusLost:connect(function()
3    if tonumber(textbox.Text) then
4        --do stuff with textbox.Text being a number
5    end
6end)

And if you wanted the players not be able to type letters, then do this.

01textbox = script.Parent.TextBox --or something
02textbox.Changed:connect(function(prop)
03    if prop == "Text" then
04        if not tonumber(textbox.Text) then
05            textbox.Text = textbox.Text:sub(1,#textbox.Text-1)
06            --using sub to delete the last thing in the string
07            --since this fires every single time they type something, it should delete letters instantly
08        end
09    end
10end)
Ad

Answer this question