I’ve noticed many advanced scripters use the self keyword within metatables, I know how metatables work but I would like to also know what self is.
self
itself means nothing, its just a word for key word used to define something (typically related to Class, or OOP) (a Variable in this sense), if you create a metatable
without using self
, it will work the exact same with no differences.
My original idea was this:
local nonmetatable = {}
local metatable = {}
setmetatable(nonmetatable, metatable)
metatable.__call = function()
print(self)
end
nonmetatable() --would this print table?
I’m pretty sure the self keyword is a keyword that references an class. It’s basically the same as just referencing the class itself. If you have experience with JavaScript, it’s like the this keyword. Just an easier way to reference a class.
You can either assign the self
variable (local self = value) or it will be automatically be assigned if it’s being referenced in a function called with a colon :
so if i did
function workspace.Part:printself()
print(self)
end
workspace.Part:printself()
would it just print “function”
It would need to be called either with a colon (I’m not 100% sure but I’m pretty sure you do need that function to be called from a table too) or you’d have to initialize it yourself.
No, it wouldn’t print anything.
The most simplistic defintion for self
is that it acts like a variable inside of a module script.
Here is an example;
local module = {
name = "name",
date = "2023",
weather = "hot"
}
function module.printSelf (self)
self = module
print(self)
end
return module
This would just print the table of the “Module” variable, wouldn’t it?
Not name, it would print the contents of the “Module” table, my apologies.
ModuleScript:
local functions = {
function workspace.Part:TeleportToPlayer(player)
workspace.Part.CFrame = player.Character.Torso.CFrame
self = functions
print(self) --prints functions inside table?
end
}
return functions
self is an automatic rblx variable for testing, you could set self to whatever you want and use it as a single variable.
Yep, you got it. Dont add “colons” btw, only do that when you are referencing another module script that has a “dot” in it.
?
That would throw an error, because your trying to print a table… It’s not you got it… Self is a automatic variable.
oh crap i didnt notice.
local functions = {}
function functions.TeleportToPlayer(self)
workspace.Part.CFrame = player.Character.Torso.CFrame
self = functions
print(self) --prints functions inside table?
end
return functions
its not gonna just print “functions” inside of the table, its gonna print the contents inside of the module script if im correct
like inside of a book
it would still throw an e r r o r …
Since functions is nil, you have to use tostring()
The variable “functions” isn’t nil since it has been assigned to a table (which holds the function)
Uhhh No it won’t? I just fixed it.
It’s still a table, which makes it an error?
No? Just because we are printing the table contents of “Module” doesn’t make it an error.
Is it something im misunderstanding?
self
is a keyword referencing the class or table itself
consider this example:
local teapots = {}
teapots.__index = teapots
function teapots:ReturnMyName()
print(self.Name)
end
function teapots.new(Name : string)
local init = {
Name = Name
}
return setmetatable(init,teapots)
end
local Teapot = teapots.new("Billy")
Teapot:ReturnMyName()
> "Billy"
you can only get self
when you call a function with the colon
Bro, go to a baseplate, insert a script, and type this:
local mytable = {'red', 'blue'}
print(mytable)
Then run it, and you get the error:
attempt to print a table
I understand nowwwjdaid !!!
you can print a table
that’s very useful for debugging