Confusion on when to use dots or colons

For example:
local player = game:GetService(“Players”).LocalPlayer
In this line of code after game we use the semi-colon and afterwards we use the dot to refer
to LocalPlayer. What exactly is the difference between a dot and a colon?

7 Likes

You will use the period . when indexing an instance. This is usually used for properties and events of an instance.

A semicolon : is used when you are using exclusively the functions of an instance.

For example, let us say we have a Part.

In the following I am reading a property of the part:

print(Part.Transparency) -->The Transparency Property of Part is read, prints 0

In the following I am using a function of the part:

Part:Destroy() -- The Destroy function is called for the Part

In the following I am hooking a function to the event of the part:

Part.Touched:connect(function()
    -- The Touched event of Part is connected to a function.
    -- When the event fires, the function will fire as a result.
end)
15 Likes

You will need to use the . when going through a instance’s path. For example: game.ServerScriptService.Script.BoolValue

However this is only when you want to change a value or refer to an instance or property, such as BackroundTransparency or Volume.

You will use : when doing a function like :Play() or :Clone(). Hope this helped!

6 Likes

Periods/dots are used when accessing the children/index of that object, whether that be a property, an event, a function, another object within or whatever else.

Colons are used exclusively for calling methods, which passes itself as a variable to that function.

As such, following the code you gave us, you can do this:

local player = game:GetService(“Players”).LocalPlayer

-- or

local player = game.GetService(game, “Players”).LocalPlayer

Note how in the latter variable definition I passed ‘game’ as an argument to that function. This is something you can recreate yourself:

local myTable = {
	favouriteValue = 0;
	anotherValue = "this table is mine!";
	print = (function (self, t)
		print("I called this function at ", t, "\n my favourite value is ", self.favouriteValue)
	end);
}

myTable:print(time())

-- or

myTable.print(myTable, time())

--[[ This will result in the following being printed:

I called this function at   0.004808
 my favourite value is  0
I called this function at   0.004909
 my favourite value is  0
]]
19 Likes

The other replies In this topic are great so far, and should answer your question, but I’m, guessing you mean colons : and not semi-colons;, because semi-colons are most commonly used to end off lines of code, other than that they aren’t used much at least in the latest versions of lua

2 Likes

Just so I understand correctly, in the line where it says “print = (function (self, t)”, its possible in Lua to set variables equal to a function? Or is that line just giving a name to the function?

Yes I meant colons not semi-colons lol.

This is a good catch - I skipped past the semi-colon remark after seeing the code provided, but yes, you’re right. In this circumstance I do think the OP meant to reference colons.

There are still some use cases for semi-colons in Lua though, even with 5.2+. Since Lua uses a freeform syntax, it is necessary to use semi-colons after defining variables before calling functions/methods and the like on the same line

2 Likes

Yes there are multiple ways to define a function:


function Hello()
end

local function Hello()
end

Hello = (function ()
end)

local Hello = (function ()
end)

local myTable = {
	Hello = (function ()
		-- stuff
	end)
}

myTable.otherHello = (function ()

end)

function myTable:methodHello()

end

function myTable.Hello()

end

They can also be defined within arrays (or tables in Lua) as seen in the example I gave before, or the final one in the series above.

2 Likes

Yes it is also necessary to use either commas or semicolons, whichever you prefer, to separate indices in an array.

1 Like