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

What do colons in variables, functions, function parameters, do?

I see people using this all the time, colons in variables, functions, function parameters, but I have no clue what they mean/do.

-- example 1:

local x : number = UserInputService:GetMouseLocation().X
-- What does the colon mean after the x?

-- example 2:
function Mouse.GetPosition() : number

-- example 3:
function Mouse.Raycast(rayParams : RaycastParams?) 

Thanks!

2 comments

0

It's apart of LuaU, in this case it's type definitions: void GetPosition(int Position) {}

0

it's lowercase u

1 answer

Luau type checking this is. It is not used to script anything but more like for organization and easier understanding of the code for others when working in teams. The way it works is that you cast class to any variable and type checker will prevent you from basic things that will cause errors - for example typing properties with lowercase etc.

Example 1

in example 1, you have type 'number', you put types after the colons, basic types are string, boolean, number, {} (table), () -> () (function), nil and thread (no idea what thread is).

Everything about it is said on Luau website here and you should check it out but i will show the 3 examples you asked. Let's say you have a variable called MyString which is a string and because you stupid you want to add 50 to it which is number, not possible right? it will error and you might only notice it when you run the game:

local MyString = 'Hello'

MyString += 50

This will give an error and as i said earlier you might not notice it in edit mode and it will then waste your time, to fix this, you use types! You give MyString a type, since it's a string, you would give it a string type:

local MyString: string = 'Hello'

MyString = MyString .. ' World'
MyString += 50 -- this will be underlined in yellow

This will underline line 4 in yellow (called type error) because it detected that MyString is string and you cannot add number to it and you will notice that you are doing something wrong. If you will try to give string wrong type, for example you give MyString type number, it will also give you a warning:

local MyString: number = 'Hello'

Because MyString is a string so it cannot be a number type. In your case x is a number because UserInputService:GetMouseLocation().X is a number and it will prevent you from doing things like this:

local x : number = UserInputService:GetMouseLocation().X

x = x .. 'Hello' -- Type warning will appear

In type checking there are 3 modes of type checking, the default one is nocheck which means that the type checker won't check anything so using types in nocheck is useless, to enable any of the modes you must type at the start of the script:

--!modename

As i said earlier, there are 3 modes and those are nocheck, nonstrict, strict. Nocheck simply won't check anything, nonstrict will check only if you have applied class to a variable and strict will automatically look for default classes and automatically apply them, don't use it tho, it's broken ah. strict:

--!strict
local MyString = 'Hello' -- you see i did not apply no classes, it does it automatically for some userdata, not for all but for most
MyString += 50 -- Will give a warning

nonstrict is all examples above except the one with strict mode, nocheck:

--!nocheck
local MyString: boolean = 'Hello'
MyString += 50
-- no reaction, nothing gives a warning

Example 2

Using types after a function is return of the function, let's say you have a function

local function MyFunction()
	return 50
end

local MyString: string = MyFunction() .. 'World'

This will error in runtime, but not in studio because type checker does not know what does the function returns, all you have to do is type it there!:

local function MyFunction(): number
	return 50
end

local MyString: string = MyFunction() .. 'World' -- will give type error

You can put unlimited return types there:

local function MyFunction(): boolean, string, number, boolean
	return true, 'string', 500, false
end

local a: boolean, b: string, c: number, d: boolean = MyFunction()

Example 3

This is similar to example 2 but instead of return it puts types on it's parameters, let's say your function accepts 2 parameters and you want to put class on them, you do that like this:

local function Multiply(First: number, Second: number): number
	return First * Second
end)

Multiply(50, 50) -- good
Multiply(true) -- bad
Multiply('string', 500) -- bad

The only difference from example 2 and example 1 is that it has ? symbol, in type checker this symbol means that it CAN be given class but CAN be nil (There are other symbols that i did not show which are: | = or, & = and), for example:

local function MyFunction(Bool: boolean?): number? -- ? symbol here
	if (Bool) then
		return 500
	end
end

MyFunction(true) -- Returns 500
MyFunction() -- Returns nil
-- None of these will give warning because first param of MyFunction has ? symbol so it does not have to be there and return argument 1 also has ? so it may return number but it does not have to

If there would not be the ? symbols, you would get type error on line 8 that you forgot to put one argument to the function, with ? symbol, it won't give type error.

Sources

I haven't showed almost anything, there are so many type functions i haven't shown like tables, function, export, type a =... and stuff but it's explained on the website and sources that i will drop below

Luau type checking

Roblox annoucement about the checking with some info about it

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