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

what does ';' do is it like ':' used to connect something?

for example in this script

I see



local Lighting = game:GetService('Lighting')

local function GetTime()
local timeFormat = "%.2d:%.2d:%.2d"
local t = tick()
return timeFormat:format(math.floor((t/3600)%24+24), math.floor((t/60)%60), math.floor((t%60)))
end

while true do
Lighting.TimeOfDay = GetTime(); -- RIGHT HERE RIGHT HERE RIGHT HERE !!!! ';' SEE THAT
wait(1)
end

2 comments

0

Habits from other languages, like Java, or C++ as examples

2 answers

✓ Accepted answer

Semicolons (;) go back a long way in terms of programming. I won't go into too much detail, as a simple Google search will get you much further. But semicolons are required in many programming languages.

Lua's syntax is so flexible that it allows for the use of semicolons if you so please. For example:

local x = 10;
print(x);

...and...

local x = 10
print(x)

...are equally valid and will compile just fine. This is great for programmers who're used to using semicolons in their code (like if they previously coded in Java or C++).

However, do not mix up semicolons (;) and colons (:). In order to understand colons in Lua syntax, you have to understand how functions work from within tables. Let's say we have a table, bar, and inside bar we have a function called foo. foo also takes two parameters: x and y. It looks a little something like this:

local bar = {}
function bar.foo(x,y)
	print(x,y)
end

Pretty standard. We can invoke this function like any other by indexing bar:

bar.foo(10,20) -- 10	20

However, what if we used : instead?

bar:foo(10,20) -- table: 0xabcdef	10

What happened there? It printed a random memory location, 10, and forgetting about the 20. If you look a little closer, you'll find something interetsing about this:

-- as previously defined
print(bar)		-- table: 0xabcdef
bar:foo(10,20)	-- table: 0xabcdef	10

As you can see, bar and the table returned by bar:foo() share the same memory location; they're the same table! This is, in fact, exactly how : works in Lua. When you call a method in Lua, the first argument in the function will be the table holding the function (in this case, bar). You may ask, "How's this useful?" Well, consider the following:

local bar = {
	x = 10; -- semicolons can also be used when defining tables instead of ","
}
function bar:foo(y) -- defining the function with : is valid
	print(self.x + y) -- the keyword "self" is used when defining functions using ":"
end

bar:foo(20) -- 30

In this example, the keyword self is the table bar. As earlier defined, bar.x is equal to 10. When we say self.x from within the function, we're referring to the value x in self, or bar.x. We're then adding the value y to self.x, which simplifies as 10+20.

I hope this clears up some confusion.

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