[Minor Topic] What is the difference between using colons in local variables?

Hey guys, this is a really minor topic but I would like to know the difference between using local Teams:Teams = game:GetService("Teams") or local Teams = game:GetService"(Teams)"?
Is there any difference or does it do something else if there is a colon there?
(Teams is an example, it can be ReplicatedStorage, ServerStorage, ect…)
Thanks!

1 Like

Unsure if I’m correct, but I’m pretty sure that defines the variable type. Observe:

local function test(variable_name : string)
end

test()

This should present the screenshot below:

Pretty sure this is just a form of typechecking, example:

local Player: Instance = Players.LocalPlayer;

To add on, I believe that you have to type “—!strict” at the top of said script to enable type checking.

You don’t need to type it. Luau already typechecks by default when there are colons

1 Like

I never seen this before, this must be a type-checking for the type of Teams.

Type-checking doesn’t change much of the baseline of the scripts. All it does is keeping the code a bit more stable or something like that. If you have your types controlled, it’ll be more controllable to catch issues if they ever appear. Typically to avoid illegible operations between two types like trying to use certain mathematical operations with strings.

1 Like

There is no difference because studio knows that the type of

local Teams

is Teams because that is what

game:GetService("Teams")

returns.


You only need it if you want to enforce that Teams's type is Teams, so that if you accidently change game:GetService("Teams") to something like game:GetService("Players") it will give a red underline in the editor.
But i don’t see how that would happen when getting a service…

I believe that the typing is mainly for variables that would otherwise be somewhat dynamic, such as how an integer can concatenate with a string, or that printing will automatically assume a string value when possible. I definitely haven’t looked into Luau typing to say anything for certain, so take this with a grain of salt. But more specifically, I think it’s primarily used for more complex and/or rapidly repeating algorithms for the purpose of optimization.

I’m not sure and I don’t have time to check, but i think it’s the same as

local Humanoid = Character:WaitForChild("Humanoid") :: Humanoid

I do this just to get the humanoid’s autocomplete stuff

1 Like

AFAIK,

The colon allows the programmer to pass the table to which the function is referenced as the first argument to the function (exactly the same as using self)

This post explains it pretty well.

I don’t think that’s the same thing as what he’s asking for
Edit: I just asked someone and he said it’s type notation I’m so I’m pretty sure what I said before is true

1 Like

From what I can see:

local Teams:Teams = game:GetService("Teams")
  • The first Teams is a global table belonging to game;
  • The second Teams is a variable belonging to the first Teams table;
  • game:GetService("Teams") is the function reference belonging to the first Teams table;

With that in mind, couldn’t you rewrite the above to be:

local Teams = function(self, game:GetService("Teams")
    return Teams
end

… where self is the Teams service?

It could be that I’m misunderstanding the question, but from what I understand, this pertains more to typechecking within Luau.

1 Like

It specifics to the type checker the variables type

With type checking the type checker warns you that you cannot add a part and a number
image

Without it the game will not warn you
image

With type checking the type check understands it is a player and autofills LoadCharacter

Again here it will not autofill LoadCharacter because we did not specify Player’s type
image

Type checking is simply a tool to help you avoid logic errors while coding. This is a luau feature and most people don’t bother using it, I personally use it sometimes if I want the game to autofill functions for me and if I’m writing documentation.

If you would like a proper tutorial for it click here

2 Likes

I’m pretty sure that would work the same way but I don’t think they’re the same thing but I don’t understand self so I’ll just shut up