#issue with OOP

1 messages · Page 1 of 1 (latest)

indigo hound
#

setting up a gun system, each gun (="tool") has a localscript which references and runs the init function of the following module:

local RS = game:GetService("ReplicatedStorage")

local module = {}

function module.init(tool: Tool)
    local plr = tool:FindFirstAncestorOfClass("Player")
    local typ = tool:GetAttribute("Type")
    local mod = RS.Modules.Weapons:FindFirstChild(typ)
    if mod == nil then return end
    mod = require(mod)
    print(mod)--[[
    ---> result:
    {
        ["__index"] = "*** cycle table reference detected ***",
        ["bind"] = "function",
        ["fire"] = "function",
        ["holster"] = "function",
        ["init"] = "function",
        ["new"] = "function", <-- IT EXISTS
        ["reload"] = "function",
        ["unbind"] = "function"
    }]]
    local new = mod.new()
    ---> result:
    --ReplicatedStorage.Modules.Weapons.Loader:12: attempt to call a nil value
    new:init(plr, tool)
end

return module

however, the line mod.new() errors despite existing

here the "new" function that i am calling:

local m = {}
m.__index = m
function m.new()
    local self = setmetatable({}, m)
    return self
end
return m

anyone sees what i'm doing wrong? thanks in advance for all input

vernal forge
#

you're probably looking at the wrong module.

#

consider the fact that the "new" function you say you are calling:

local m = {}
m.__index = m
function m.new()
    local self = setmetatable({}, m)
    return self
end
return m```
does not contain anything else such as bind, fire, holster etc
#

it's also interesting to me that loader:11 is printing twice, the first one doesn't error but the second one does?

#

you doing some weird things bruh mod = require(mod)

indigo hound
#

i have now partially fixed the issue by commenting m.__index = m and doing local self = m instead of a metatable

indigo hound
vernal forge
#

they're probably conflicting with each other in some way

vernal forge
#

and you have like 2 modules that do the same thing, or something?

#

going purely by the code you show i dont see any issue but who knows what you're doing at this point

indigo hound
#

hmmm

vernal forge
#

so god only knows what's going through your head

indigo hound
vernal forge
indigo hound
#

i now did disable the currently unnecessary ones

#

let me re-run with new changes

#

ok this is strange

vernal forge
indigo hound
#

i undid the removal of the __index line and {} instead of a metatable and it seems to be working now (as in that "new" isn't nil)

simple ravineBOT
#

studio** You are now Level 1! **studio

vernal forge
#

you're doing very strange things so expect strange results 👍

indigo hound
#

okay i see

vernal forge
#

great now i'm hungry brb

indigo hound