What does ':' do specifically?

  1. What do you want to achieve?

I want to learn what ‘:’ does.

  1. What is the issue?

I had randomly tried putting ‘:’ after a parameter in a function and it gave me these options such as String, Color3, etc. When I had then used the function, I found out that it defines what the value should be. But I still don’t know how to properly utilise this.

  1. What solutions have you tried so far?

I have searched the devforum if there are topics that explain this, but I found nothing but random stuff about functions.

EDIT 05/04/2022: Sorry for not clarifying but I have now found a solution which explains exactly what I meant.

To begin with, I can not recreate what you are stating from what I understand is you were defining a path then but a colon and got the change to put a Color3 value? So something like… game.Workspace.Baseplate:Color3()… I was not able to directly recreate this if you could show me more of what you mean it would be create. But from what I have seen a “:” simply is defining a function so for example :Destroy() or :Clone(). Then when you use a ‘.’ you are defining a path so… game . Workspace . Baseplate. So to delete the baseplate from the workspace you would use a period to define the path game.Workspace.Baseplate then a colon to use the Destroy function… game.Workspace.Baseplate:Destroy(). If you need me to explain it further I can.

I recommend reading this reply; it’ll help you understand a bit:

Are you referring to the Luau typechecking? Roblox has extensive documentation of that.
It’s primarily used to help catch coding errors in real-time, as by defining explicit types the script analysis can predict what is required and what is incompatible.

An example of using it would be:

type requires<T, O...> = (T) -> (O...)
local factorial: requires<number, number> = function(n)
	local prod: number = 1
	for i = 1, n do
		prod *= i
	end
	return prod
end

--these messages will pop up in the script analysis window warning you of inappropriate usage
print(factorial('hi')) --> Type Error: (10,17) Type 'string' could not be converted into 'number'
print(#factorial(5)) --> Type Error: (11,7) Expected type table, got 'number' instead

Although a more common way of writing it is

local function factorial(n: number): number
...
1 Like

A colon usually means a function call, where a normal . references a child or property of an item. For example,

game:GetService() -- calls the get service function of game

game.Players -- references the child of game, the players service. You normally would call it with the previously mentioned GetService() function but this is just an example.

If you are referring to calling functions as members of some object like a table or userdata, you can use either, depending on how the parameters are handled. They both do the same thing, but calling a function with : passes in the object as the first argument. This allows you to call functions on specific objects as if they are methods.

local object = {["p"] = print}
object.p("hello") -- prints "hello"
object:p("hello") -- prints "table: 0x... hello"

It can be used in defining the function as well

local object = {}
function object.a(self, ...)
print("self:", self)
print("args:", ...)
end
function object:b(...)
print("self:", self)
print("args:", ...)
end

Calling object.a(object, 1, 2, 3) is the same as calling object:a(1, 2, 3) is the same as calling object:b(1, 2, 3)

It is also important to know this when using things like pcall statements. If you are wrapping a method of a Roblox service, for example, you would have to include the service in the list of arguments, since calling service:method(args) is like calling service.method(service, args) (although when invoking service methods directly you can only use the colon)

local success, results = pcall(function() return HttpService:GetAsync("url") end)
-- is equivalent to:
local success, results = pcall(HttpService.GetAsync, HttpService, "url")

Yes, this is what I had meant, I should find all the information on that with that documentation. Sorry I didn’t clarify what I had meant.

Examples

whatever_your_model_is:Destroy()
players_name:Kick("test")
path_name:WaitForChild("test")
game.Players.PlayerAdded:Connect(function(plr: Player)

basically a function call