Luau Type Checking - Number of certain length

Hello, I’m wondering if three’s a possible way to add a luau type check of a number that has to be 3 digits, no less, no more.

Thanks!

1 Like

if number > 99 and number < 1000 then

if math.floor(number) > 99 and math.floor(number) < 1000 then
   -- do something
end

missed floor, thanks for clarifying!

1 Like

you could also do

if string.len(tostring(number) = 3 then
    --do something
end

Oh sorry, I’ve changed my mind of what I want to do, thinking about it now.

What I want to do is a luau type check that underlines the argument if the index of that argument is not found inside of a table…

the issue escalated quickly

what do you mean by underline?
what do you mean by index
if an index of an argument exists in a table, then it will be found in that table right?

Something like this.

local Codes = {
	["111"] = {
		CoolThing1 = "145",
		CoolThing2 = "525"
	},
	
	["222"] = {
		CoolThing1 = "123",
		CoolThing2 = "444"
	},
}

local function DoCoolThing(Code: number) --Change number. I want to change number so it underlines the Code parameter if it is not found inside of the table
	print(Code)
end

So basically if “Code” is not “111”, “222” or any number thats found inside that table then “Code” will underline orange.

So like I could do Code: “111” | “222” but I don’t want to do that for each time I add a new table I just want it to check without adding it.

Sorry, this was sort of rushed because I’ve got to go in a minute so my explanations not the best.

local newthing = {}
local function findstuff(thing)
    for _, thing2 in pairs(thing) do
        table.add(tostring(_), newthing)
        if type(thing2) = "number" then
             table.add(tostring(thing2), newthing)
        elseif type(thing2) = "string" then
             table.add(thing2, newthing)
        else
              findstuff()
        end
    end
end

note, this will definitely not work first try i am not that good at coding

oh yeah almost forgot dont forget to run the function with “codes”

Do you want to type check numbers that are like 333, 555, and 888?

Yeah, but without having to add each number everytime i add a new thing

Not possible as of now. You can’t even annotate number literals, only strings and booleans can have literal types.

local a: "hello" = 'hello' --allowed
local b: true = true --allowed
local c: 1 = 1 --not allowed! this will cause an error

Yeah you can’t like what @Prototrode said. What you can do is check if number is not in the table then error.

local Codes = {
	["111"] = {
		CoolThing1 = "145",
		CoolThing2 = "525"
	},
	
	["222"] = {
		CoolThing1 = "123",
		CoolThing2 = "444"
	},
}

local function DoCoolThing(Code: number)
	if not Codes[tostring(Code)] then error("Number is not in table") end
	print(Code)
end

The answer is no. Typechecking is exactly that, it checks types not value.

Moreover, luau typechecking is just a guide. It won’t block your code from being executed even if the types are wrong. All luau type annotations are completely ignored at runtime, at least right now. Otherwise, it would slow down execution.

Just to clarify, semantical errors such as mismatching types are ignored, but compilation (syntax) errors such as trying to annotate a number literal aren’t :slightly_smiling_face:

IIRC some Roblox engineer said one of the end goals of the type system is actively using it to generate more efficient bytecode or machine code. So there is some intention in the near future where it does gets a say in the compiler

That’s just for native code generation though. So unless your file is annotated as native it won’t do anything. And that’s a beta feature so it’s out of scope for this post.

The answer is still No to OP’s question and the luau type annotations are still discarded at runtime.