How can i make a command that only works if you pick a number not a letter

Sorry, pretty new to admin system making. Just wondering how i can make a command only work if the argument is a number not a letter

example:

:number 5

input: 5

:number hi

input: CANNOT USE LETTERS

You would use string patterns. The pattern for this case would be “%d” and you would put a + for getting multiple numbers, making it “%d+”

local _string = 'testing123 testing321'
local numbers = {}

for number in string.gmatch(_string,"%d+") do
	table.insert(numbers,number)
end

print(numbers)

didnt really understand, im asking like a detection this seems like just getting the number in the message

like what if i go like

:number 500HI

it should say “Error, command only works with NUMBERS only”

If you run the code I posted above in command bar, you can see it prints a table full of the numbers found in the string.
You would use the string pattern “%d” to see if the string has numbers in it.

local _string = ':number 500HI'
local hasNumber = string.gmatch(_string,"%d")()

if hasNumber then
     --Run the command or whatever
end

If you want to see the kinds of string patterns you can use, they are on the wiki.

Sorry idk if i just cant seem to understand you

the post reads ONLY if the argument given is a NUMBER, NO letters at all

Oo my bad, I thought you wanted to just know if it had any numbers in it. Now it’s super simple, you can just use “tonumber()”

local _string = ':number 500HI'
local isNumber = tonumber(_string)

if isNumber then
     --Run the command or whatever
end

If the string has anything else in it, the “isNumber” variable will equal nil

2 Likes

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