I have a TextBox, that can be edited. I have a problem though, I want it to be editable, but numbers only. I tried using :Tonumber as suggested by someone (I forgot who), but apparently didn't work...
OUTPUT
Players.Player1.PlayerGui.ScreenGui.GemsToDiamonds.Custom.S:5: attempt to call method 'Tonumber' (a nil value)
What I want is A TextBox be able to type numbers only, then put that number as a Value in TextBox. But is didn't work, someone help please?
I think that there is an easier way to check for this, but I can't remember what it is.
local function is_numbers(txt) for i in string.gmatch(txt, "%a") do -- %a is the patter that looks for any upper or lower case letter. -- Immediately return false, if we find a letter. return false end -- If there was no letter, then you're good to go. return true end print(is_numbers("99999")) -- Will print true, because there were no letters. print(is_numbers("stri9ng")) -- Will print false, because there was a letter. if is_numbers("99999") then print("Everything in the string was a number") else print("There was a letter in the string") end
--[[ Example ]]--
local txtbox = script.Parent:WaitForChild("TextBox") local function is_numbers(txt) for i in string.gmatch(txt, "%a") do return false end return true end txtbox.Changed:connect(function() local store_text = txtbox.Text -- Store the current text in a variable, it's just personal preference, it looks cleaner imo. if not is_numbers(store_text) then -- If what you just typed isn't a number then txtbox.Text = store_text:sub(1, store_text:len() -1 ) -- Text = current text - the letter you just typed. end end)