I am pretty new to OOP, and i keep giving me an error i have no idea what i am doing wrong to make it occur.
local PartTouched = {}
PartTouched.__index = PartTouched
function PartTouched.new(obj)
local self = {}
self.Part = obj
self.Connection = obj.Touched:Connect(function(hit)
local Char = hit:FindFirstAncestorWhichIsA("Model")
if Char then
local Humanoid = Char:FindFirstChild("Humanoid")
if Humanoid then
PartTouched:Ignite()
end
end
end)
return setmetatable(self, PartTouched)
end
function PartTouched:Ignite()
print(1)
self.Connection:Disconnect() -- Location where it occurs.
self.Part:Destroy()
end
return PartTouched
This just calls the method function PartTouched:Ignite() but self is not passed so Connection will always be nil, since it doesn’t exist.
Iirc, PartTouched.Ignite(self) should work or self:Ignite(), but you should probably set the metatable before calling any of the methods.
local PartTouched = {}
PartTouched.__index = PartTouched
function PartTouched.new(obj: Part)
local self = {}
setmetatable(self, PartTouched)
self.Part = obj
self.Connection = obj.Touched:Connect(function(hit)
local Char = hit:FindFirstAncestorWhichIsA("Model")
if Char then
local Humanoid = Char:FindFirstChild("Humanoid")
if Humanoid then
self:Ignite()
end
end
end)
end
function PartTouched:Ignite()
print(1)
self.Connection:Disconnect()
end
return PartTouched
function PartTouched.new(obj: Part)
local self = {}
setmetatable(self, PartTouched)
self.Part = obj
self.Connection = obj.Touched:Connect(function(hit)
local Char = hit:FindFirstAncestorWhichIsA("Model")
if Char then
local Humanoid = Char:FindFirstChild("Humanoid")
if Humanoid then
self:Ignite()
end
end
end)
return self
end