-- Main Script
LogService:AddLog("Chatlogs", Message)
-- Module Script
function LogsModule.AddLog(LogName, Element)
print(LogName, Element)
if #LogsStore[LogName] == SystemSettings[4] then
table.remove(LogsStore[LogName], 1)
end
print(LogName, LogsStore[LogName])
table.insert(LogsStore[LogName], Element)
end
When I run AddLog from main script I get the following to print:
- table: 0xe8cd682c3a62f63c Chatlogs
The first argument is evidently a string, why does it turn to a table?
Thanks.
(Update: using . instead of : seems to fix it, why??)
Calling a method of a table with : implicitly passes the table itself as the first argument, so it’ll replace the first formal parameter of the function. (If you defined the method/function using the dot operator)
Defining a function in a table with : handles it implicitly, so basically this is how it’ll look:
function myTab:something(self, ...) --//self is implicitly added as a formal parameter
end
Whenever you call a method using a colon ‘:’, as in “Part:Destroy()”, it passes the table / module itself as the first argument, so when you call LogsModule:AddLog() (with a colon), the first parameter will be LogsModule, and the second will be the message, when you call with a period rather than a colon it will not send the table/module so the first parameter will be the message.
I recommend reading posts on the usage of the colon. There’s plenty of good ones.
This is known as ‘syntactic sugar’, where a programming language takes liberty in making things easier to read and express.
There is no effective difference between these two things:
local a = {}
function a:foo()
print(self)
end
function a.bar(self)
print(self)
end
a:foo() -- self implicitly passes
a.bar(a) -- self manually passes
Also for the sake of learning, self isn’t a keyword regardless of how it is highlighted because it doesn’t actually reserve space… it’s just another variable that syntactic sugar has the ability to implicitly pass.