#metatables n stuff help im going insane

11 messages · Page 1 of 1 (latest)

royal fulcrum
#

so im making a npc class with oop and and ive already done all its functions(moving, eating, etc.) but im struggling with the constructor's function where it creates a new npc and makes it do the stuff automatically because in that function i implement npc's class methods to the npc model, until here, everything is fine, but then, as the 1st argument for _init() i must input the model, but if i do, for some reason, it says the methods arent there, so i tried, instead, inputting the metatable, which ALMOST works, because when i try using builtin model class' methods it says "expected : not . calling member function [function name]" (ive read abt this here: https://devforum.roblox.com/t/expected-not-calling-member-function/31935 but i still dont understand), heres an example:

local npc = {}
npc.__index = npc

local function _init(model)
    --here i have an ai which automatically moves the npc around and makes it eat if food is found in front of him
end

function npc:move(position: Vector3)
    --move it
end

function npc:jump(force: number)
    --make it jump
end

function npc.new(name: string, position: Vector3)
    local model = script[name]:Clone()
    --do some stuff with the model such as naming it, positioning it and parenting it n other stuff
    
    model = setmetatable(npc, {__index = model}) --works but all model's built-in functions error: "expected ':' not '.' calling member function [function name]" and i cant just use the actual model since it then doesnt 
    
    _init(model)
    return model
end

return npc
dense burrow
#

so where is it erroring

#

way too much text for a simple error like this

#

just say the line

royal fulcrum
# dense burrow so where is it erroring

if u read the whole message u can see theres not actual error, the problem is that if i do setmetatable(npc, {__index = model}) for applying the methods it works BUT if i use that metatable for using the object model's methods it says "expected : not . calling member function" , if i try using the actual model object it says that the methods i made are missing, so, the only way to fix this, is to implement all default model's methods into that metatable, which is what im working on

dense burrow
#

so what would error

#

you are getting an error

royal fulcrum
dense burrow
#

and what would error

#

what would you be doing

#

what line