How to make number only textboxes!

Welcome all!

today I will be showing you how to make number only textboxes!

For starters your going to need a script and a textbox!
I’m just going to put the script into the textbox!

Now lets begin!

For starters we need to make a variable for the textbox

local textbox = script.Parent -- change this to your textbox.

now we are going to need another variable, this one is going to be used to change the text back if the text is not a number.

local lastText = '' -- we set this to empty.

Now we need to be able to tell if the text is changed that way we can make sure it’s a number!

local textbox = script.Parent

local lastText = ''

textbox:GetPropertyChangedSignal('Text'):Connect(function() -- this funcion runs when you start typing

end)

now we need to check if the text is a number.

if you are wondering why we also check if textbox.Text == ‘’, this is so we can tell if its set to nothing. If we dont you will not be able to remove all the text from textbox while your typing.

local textbox = script.Parent

local lastText = ''

textbox:GetPropertyChangedSignal('Text'):Connect(function() -- this funcion runs when you start typing
      if textbox.Text == '' or tonumber(textbox.Text) then -- we use "tonumber" to see if its a number
      end
end)

Now we need to check if its not a number that way we can revert it back to its last text! And we also need to change the “lastText” variable to the text when it is a number or nil,

local textbox = script.Parent

local lastText = ''

textbox:GetPropertyChangedSignal('Text'):Connect(function() -- this funcion runs when you start typing
      if textbox.Text == '' or tonumber(textbox.Text) then -- we use "tonumber" to see if its a number
             lastText = textbox.Text -- set the text so we can revert it later
      else -- this runs if its not a number or its equal to nil
              textbox.Text = lastText -- we use this to change it back to its last text
             print('Player is typing a letter')
      end
end)

And that’s it! I tested this out and it works perfectly!

If you have any questions regarding this feel free to reply!
And that’s all folks. PEACE

video of it working:

15 Likes

That would fit better in #resources:community-tutorials

1 Like

Oh thanks!
Sorry i’ve never made a tutorial before

1 Like

Yes very true.

This is my first tutorial so yeah

1 Like

Yes, it’s simpler, but it won’t work as the previous post mentioned. You see, if I have a number like 123 and I delete the 2, which is between 1 and 3, it will only delete the 3 because it’s the last number.

2 Likes

Wouldn’t

textbox:GetPropertyChangedSignal("Text"):Connect(function()
textbox.Text = textbox.Text:gsub("%D","")
end)

also work?

5 Likes

If you change the pattern to something like [^%d.-] then it will also support decimal and negative numbers.

1 Like

I advise against doing this every time the textbox is changed. It will just annoy people. To type a negative number or a decimal, I’d need to first type all of the numbers and then add the negative or decimal symbol.
-4.5e-3 is also a valid number in Roblox, but to type it in I would need to first type 453 and then add the first negative and the decimal, and then paste in the e- combo. Hexadecimal would be impossible.

Textbox filters should run on FocusLost instead.

The problem here obviously is that it also supports non-numbers like -5-5…7.-. It’s a decent hybrid solution if you let it run on changed and use tonumber on focus lost. But if you wanted the full functionally to exist, you’d also need to allow a few more symbols: [%.%-%dxA-Fa-f]*

2 Likes

For anyone who does come here please do use this its much simpler

:smiley:

1 Like

estoy diseñando un sistema de adicion de musica con el ID tomado desde un TextBox, quisiera que ese “string” se transforme a un valor numerico, que mantenga su valor, pero que ahora sea un numero.