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.

01local NPC= {}
02NPC._index = NPC
03 
04function NPC.new(model)
05    local entity = setmetatable({}, NPC)
06    for index, value in pairs (NPC) do
07        entity[index] = value
08    end
09    entity.Self = model
10    entity.Settings = model:WaitForChild("Configurations")
11    entity["Mind"] = model.Mind
12    return entity
13end
14 
15function NPC:GetCFrame()
View all 29 lines...

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