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.
COMMUNITY QUESTION
How do I make a TextBox only accept numbers?
1 answer
M39a9am3R is right.
--tonumber will turn a string into a number or return nil if the string is not a number
tonumber("hello!")--nil
tonumber("1244")-1244
Then to have the TextBox only use numbers, just do something like this.
textbox = script.Parent.TextBox --or something
textbox.FocusLost:connect(function()
if tonumber(textbox.Text) then
--do stuff with textbox.Text being a number
end
end)
And if you wanted the players not be able to type letters, then do this.
textbox = script.Parent.TextBox --or something
textbox.Changed:connect(function(prop)
if prop == "Text" then
if not tonumber(textbox.Text) then
textbox.Text = textbox.Text:sub(1,#textbox.Text-1)
--using sub to delete the last thing in the string
--since this fires every single time they type something, it should delete letters instantly
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Have the script check if tonumber(string) ~= nil. tonumber will return nil if the string given is not a number at all.