------------[ Module script ] ------------
local tweenService = game:GetService("TweenService")
local door = {}
door.__index = door
door.closed = false
door.closeTime = 2
function door:New(model)
local newDoor = setmetatable({}, self) -- Here
newDoor.__index = newDoor
newDoor.Model = model
return newDoor
end
function door:CreateTween(properties)
local tween = tweenService:Create(
self.Model.Door, -- Here
TweenService.new(self.closeTime, --Here Enum.EasingStyle.Quad, Enum.EasingDirection.In),
properties
)
return tween
end
function door:ToggleState()
self.closed = not self.closed -- Here
local properties = {}
properties[true] = {Position = self.Model.ReferencePoints.Closed}
properties[false] = {Position = self.Model.ReferencePoints.Open} -- Here
local tween = self:CreateTween(properties[self.closed]) -- Here
tween:Play()
end
return door
------------[ Server script ] ------------
local module = require(game.ServerScriptService.ConfigModule)
local collectionService = game:GetService("CollectionService")
local tagged = collectionService:GetTagged("Door")
for _, door in pairs(tagged) do
local newDoor = module:New(door)
newDoor:ToggleState()
newDoor:ToggleState()
end
Please tell me what value self contains in each case.
So you don’t have to give me an example of each case, just tell me what self means in each function, that is all I want to know.
E.g.
Function 1: Contains the door table
Function 2: Contains the newTable in the server script
Looking at the code, the author intends for the developer to call the door:New method first and use the functions on the returned value for that (This is common in OOP).
This is what I mean:
local doorData = door:New()
doorData:CreateTween(...)
It depends on how you structure the code afterwards, a slight change to the code can technically allow you to use the code without self, but an incorrect modification can break the system. So, yes
In simpler terms, if you call a function with a colon (:), self is automatically passed, but if you call a function with a dot (.), self isn’t passed (and in that case, if you need self, you would need to pass it manually)
local Door = {} --class
Door.__index = Door --check class for key/field if object doesnt have it (essentially making the object inherit from the class)
function Door.new(name, size, material) --class constructor function
local NewDoor = {} --door object
setmetatable(NewDoor, Door) --sets object's metatable to the door class (essentially making it an object of the door class)
NewDoor.Name = name --assign custom properties
NewDoor.Size = size
NewDoor.Material = material
NewDoor.Open = false --default property
return NewDoor
end
function Door:open()
self.Open = true
end
function Door:close()
self.Open = false
end
local door = Door.new("RandomDoor", 10, "Wood") --create door object
door:open() --call open instance method on the created door object
print(door.Open) --true
local door2 = Door.new("RandomDoor2", 10, "Wood") --create 2nd door object
print(door2.Open) --false
Here’s an example I just wrote using your door example, hopefully it should help clear things up a little.
We have a door class constructor function which can be called to create door objects, following this we have two methods “open” and “close” which can be called on door objects in order to open/close them respectively.
function door:CreateTween(properties)
local tween = tweenService:Create(
self.Model.Door, -- Here
TweenService.new(self.closeTime, --Here Enum.EasingStyle.Quad, Enum.EasingDirection.In),
properties
)
return tween
end
And it doesn’t make sense. If self is door how would self.Model.Door work if Model isn’t associated with the door table?