How to detect a number from a message

Hello everybody! I am currently making a command script. However for some reason the chat cannot detect a number in the message I sent.

game.Players.Rescripted_Developer.Chatted:connect(function(msg)
			if msg == "/FPS"..tonumber(msg) then
				script.FPS.Value = msg
			end
			if msg == "/Quality"..tonumber(msg) then
				script.Quality.Value = msg
			end
		end)
1 Like

can you type out an example string and point out which section of numbers you are trying to extract

Alright heres an example,

my message would be “/FPS(number)”

and I would be trying to extract the number.

1 Like
local NumberFromString = string.match("The Cloud Kingdom has 25 power gems", "%d+")

-- prints "25" so you write this:

local NumberFromString = string.match(msg , "%d+")
local msg = "/FPS17"
local command, value = string.match(msg, "/(%a+)%s*(%d+)")
print(command)
print(value)
if (command == "FPS" and value ~= nil) then
	script.FPS.Value = value
elseif (command == "Quality" and value ~= nil) then
	script.Quality.Value = value
end

It didn’t work. Its just giving me nil outputs.

did you try the code i sent???

I tried that too. However Im new to using strings and I do not know how to format it. I edited the 3rd post to make myself more clear.

You can use string.sub

local msg = "FPS1000000"
local sub = string.sub(msg,3)
print(sub)
if sub == "1000000" then

end

That might work, but make sure to not remove the print just in case I might not be accurate.

I think this is getting confusing, what I want to do is make it so that I can input ANY number along with a command. For example

“FPS/(number)”
and then the number would be printed out.

game.Players.Rescripted_Developer.Chatted:connect(function(msg)
	local NumberFromString = string.match(msg , "%d+") 
	if msg == "/FPS"..NumberFromString then
		script.FPS.Value = NumberFromString
	end
	if msg == "/Quality"..tonumber(NumberFromString) then
		script.Quality.Value = NumberFromString
	end
end)

i think this is what you are trying to achieve
(edit)
or this sorry didnt see u were concatinating a tonumber

game.Players.Rescripted_Developer.Chatted:connect(function(msg)
	local NumberFromString = string.match(msg , "%d+") 
	if msg == "/FPS"..tostring(NumberFromString) then
		script.FPS.Value = NumberFromString
	end
	if msg == "/Quality"..tostring(NumberFromString) then
		script.Quality.Value = NumberFromString
	end
end)
2 Likes