Hello guys I am making a gun system. but for some reason metatables are connected between them
let me… explain haha.
this is my example code;
local Base = {}
Base.__index = Base;
function Base.new(X)
local self = setmetatable(Base,{})
self.hello = X
return self;
end;
function Base:Print()
print(self.hello)
end
return Base
as you can see there’s nothing wrong with this… but…
when I run this piece of code:
local test1 = Base.new("hi")
local test2 = Base.new("bye")
test1:Print() -- prints bye
test2:Print() -- prints bye
So what’s actually happening is that you set the metatable of Base to an empty table. Close, but you actually want to set the metatable of the blank table to Base, so the opposite needs to happen so you get a new unique table each time. Otherwise, you just return Base each time the new constructor gets called. (hence modifying the hello field in the same table)
local self = setmetatable({}, Base)
self.hello = x
return self
Oh thanks so much!
I literally had 2 modules (Since I was doing inheritance method)
And I thought something was wrong with that… can’t believe the error was just this (I deleted my inheritance module and wrapped everything in one