Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Rbx Lua Classes can't grab models?

Asked by 8 years ago

The error is that the values assigned in the new method are nil to other methods of the class. How can I have the values not be nil when a method requests for them? As when I spawn a NPC and ask for the CFrame, it states self is nil.

local NPC= {}
NPC._index = NPC

function NPC.new(model)
    local entity = setmetatable({}, NPC)
    for index, value in pairs (NPC) do
        entity[index] = value
    end
    entity.Self = model
    entity.Settings = model:WaitForChild("Configurations")
    entity["Mind"] = model.Mind
    return entity
end

function NPC:GetCFrame()
    local humanoidRootPart = self.Self:FindFirstChild("humanoidRootPart")
    if humanoidRootPart and humanoidRootPart:IsA("BasePart") then
        print(humanoidRootPart.CFrame)
        return humanoidRootPart.CFrame
    else
        return CFrame.new()
    end
end

function NPC:Initialize()
    NPC:GetCFrame()
end

return NPC

1 answer

Log in to vote
0
Answered by 8 years ago

The issue was - shockingly - due to the the metatable being set incorrectly. It was: setmetatable(NPC, {}) which causes the methods to be able to grab their own values.

0
So 'entity' was set wrong? Just clarifying, I am personally trying to learn how to use metatables. TheDeadlyPanther 2460 — 8y
0
Uh? User#6546 35 — 8y
Ad

Answer this question