|
| 18 Jul 2016 10:02 AM |
How do I make it so you can only type numbers in a textbox?
#code Instance.new("Explosion",workspace["mind blown"]) |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 10:05 AM |
if tonumber(TextBox.Text) then -- Do stuff end
That is the best and easiest way, in my opinion. You can make it so that it erases while typing, but that requires complicated string patters that I don't really want to think through right now.
|
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 10:10 AM |
To remove all non-numerics from a string:
str = str:gsub('%D','') |
|
|
| Report Abuse |
|
|
0Dan
|
  |
| Joined: 22 Oct 2009 |
| Total Posts: 2552 |
|
|
| 18 Jul 2016 10:11 AM |
If you want an auto updating number only box you could do something along the lines of this
local last = script.Parent.Text while wait(.1) do if(tonumber(script.Parent.Text))then last = script.Parent.Text else script.Parent.Text = last if(tonumber(last))then else script.Parent.Text = "" end end end
𝐃𝐚𝐧𝐢𝐞𝐥 |
|
|
| Report Abuse |
|
|
0Dan
|
  |
| Joined: 22 Oct 2009 |
| Total Posts: 2552 |
|
|
| 18 Jul 2016 10:11 AM |
darthpyro method is actually a lot better, use that.
𝐃𝐚𝐧𝐢𝐞𝐥 |
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 10:12 AM |
textbox.Changed:connect(function(property) if property == "Text" then textbox.Text = textbox.Text:gsub('%D','') end end) |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 10:13 AM |
But then they can't use anything but positive integers, and they can't use things like "1e-05". As I said, my way is probably the easiest.
|
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 10:14 AM |
No!!! DarthPyro's is NOT better! Just go with mine. It won't erase your whole input if you make a mistake while typing, as it should only be run after they press enter.
|
|
|
| Report Abuse |
|
|
0Dan
|
  |
| Joined: 22 Oct 2009 |
| Total Posts: 2552 |
|
|
| 18 Jul 2016 10:14 AM |
Hm, then the method is really up to op depending on the kind of numbers he'll be using
𝐃𝐚𝐧𝐢𝐞𝐥 |
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 10:19 AM |
To catch negative numbers and exponentials as well:
script.Parent.Text = script.Parent.Text:gsub('[^%d\-\+e\.]','')
You'd obviously have to do some sort of control to ensure they enter a number and not an expression. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 10:20 AM |
But then they could break your script with -5....-++-+5ee3.5e5
|
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 10:21 AM |
And you used backslashes instead of percents.
|
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 10:22 AM |
| As I said, you would have to have some sort of control. They can break your script, as well. If I spend more time on the regex we can even limit them to a single e, single point, etc. |
|
|
| Report Abuse |
|
|
0Dan
|
  |
| Joined: 22 Oct 2009 |
| Total Posts: 2552 |
|
|
| 18 Jul 2016 10:23 AM |
I may be wrong, but I'm pretty sure ot just wanted _basic_ numbers.
𝐃𝐚𝐧𝐢𝐞𝐥 |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 10:24 AM |
That's kind of my point though. tonumber is the easiest.
|
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 10:25 AM |
| But possibly not the most ideal. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 11:12 AM |
This is more ideal I suppose?
local In = "+-+-1...5.9eefhg-+-5.00-;4" local Out = ""
local Last = "None" local Pattern = { ["None"] = function(C, N, I) if C:match("%d") then Last = "Number" elseif C:match("[-+]") then Last = "Sign" elseif C == "." then Last = "Point" else return nil end return C end, ["Sign"] = function(C, N, I) if C:match("%d") then Last = "Number" elseif C == "." then Last = "Point" else return nil end return C end, ["Number"] = function(C, N, I) if C:match("%d") then elseif C == "." and (N:match("%d+.-[eE].-%d+", I) or N:match("%d", I)) then Last = "Point" elseif C:lower() == "e" and N:match("%d+", I) then Last = "E" else return nil end return C end, ["Point"] = function(C, N, I) if C:match("%d") then Last = "Number2" else return nil end return C end, ["Number2"] = function(C, N, I) if C:match("%d") then elseif C:lower() == "e" and N:match("%d+", I) then Last = "E" else return nil end return C end, ["E"] = function(C, N, I) if C:match("%d") or C:match("[-+]") then Last = "Number3" else return nil end return C end, ["Number3"] = function(C, N, I) if C:match("%d") then return C else return nil end end } for Index = 1, Num:len() do local Result = Pattern[Last](Num:sub(Index, Index), Num, Index) if Result then Out = Out .. Result end end print(Out)
The problem is, unless I modify it a bit, it won't let you type correctly in to a TextBox. And this is why I loath your idea dude. Not only is this way messy to get it syntactically correct, but I've seen it quite often with bad GUIs. Trying to enter a number in? Trying to make a value 500? Let's erase all of the numbers in the TextBox to start with 5 to spell out 500. Oh what's that? The minimum value is 10? Now, thanks to your code correcting your "mistakes" after every single keystroke, you can't type in 500 without it reverting to 10. I absolutely loath TextBox management like that.
|
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 11:13 AM |
My bad, the top value "In" should be renamed to "Num". I deleted that line to try to fit it in to a TextBox Changed event, before realizing why that wouldn't work, hence the paragraph at the bottom.
|
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 11:15 AM |
And a small correction to the "None" function to make output prettier:
["None"] = function(C, N, I) if C:match("%d") then Last = "Number" elseif C == "-" then Last = "Sign" elseif C == "." then Last = "Point" else return nil end return C end,
|
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 11:22 AM |
| The regex simply disallows you from typing invalid characters. I don't see what the issue is. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 11:41 AM |
My issue is that it isn't perfect. Why do you need to do that when you would still need to filter out the string? Either way, tonumber is (for the most part) required, and simpler when you use only tonumber.
|
|
|
| Report Abuse |
|
|
|
| 23 Jul 2016 05:19 PM |
They're only limited to a value that is 1-6...
#code Instance.new("Explosion",workspace["mind blown"]) |
|
|
| Report Abuse |
|
|