Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why won't this work?

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?

1 answer

I think that there is an easier way to check for this, but I can't remember what it is.

string.gmatch

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)


3 comments

0

Or simply just "return string.match(txt, '%d+') ~= txt"

0

' I think that there is an easier way to check for this, but I can't remember what it is. '

0

Confusing, Also, I can't see how I can set the number in the textbox as a value (NumberValue) as the Textbox's Child. So I have a textbox, and in it I have a script and the Value.

This discussion is preserved as it was. New answers, comments, and votes are closed.