Do not understand string pattern documentation/how to make a string *only* contain numbers

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a radio callsign input, but I want it to be only numbers.
  2. What is the issue? Include screenshots / videos if possible!
    I simply do not understand the material that’s been given for me to ‘study’ (sounds stupid, i know right?)
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have checked the good ol outdated String Patterns (roblox.com) doc, but i couldn’t understand that (nor find a up to date version !)

if your wondering “bro, what are you using right now?”, here. game.ReplicatedStorage.GameplayUIs.Radio.getCS:InvokeServer(true, string.%d(script.Parent.CallInput.Input.Text)) – expected identifier got %

You could filter the TextBox itself so it only allows numbers as input. Add a local script as a child of the TextBox and use this script:

local textBox = script.Parent

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	textBox.Text = textBox.Text:gsub('%D+', '');
end)

You could also just put to the same gsub on the TextBox.Text like so:

script.Parent.CallInput.Input.Text:gsub('%D+','')

I do not understand the last bit of what you wrote, but I’m guessing that you want to have a string that contains only numbers, like “1234,” and parse it to get a specific response. I see that you did string.%d, which does not make any sense. %d is a class modifier that will return the first number it sees when something like:

local matchA = string.match("1234","%d")
print(matchA) -- prints 1

In short, your syntax was wrong.

Random question. But is there a reason why your using string patterns vs something like tonumber?

Because if I am reading this post you want a string that is only numbers, and I’m guessing from a text box. So is there a reason your using the string patterns. I like to use tonumber personally for this.

In this example here, I have a text box, which the text turns red if its not a number, or it turns green if it is number, here is the code if you wanna try it:

Code
local textbox = script.Parent

textbox:GetPropertyChangedSignal("Text"):Connect(function()
	local IsItANumber = tonumber(textbox.Text)
	if IsItANumber then
		textbox.TextColor3 = Color3.new(0.0666667, 1, 0)
	else
		textbox.TextColor3 = Color3.new(1, 0, 0.0156863)
	end
end)

So again, is there a reason why you prefer string patterns? Sorry if this seems like a dumb question, I myself am not into string patterns too much

(Also here is a link to string patterns that are more updated on the roblox documentation page as well: string patterns - roblox docs )

i thought tonumber would be like int(string) in python, where if you have a letter in the string it will throw a error and the code will give up.
ill try it :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.