Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Rbx Lua Classes can't grab models?

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

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.

2 comments

0

So 'entity' was set wrong? Just clarifying, I am personally trying to learn how to use metatables.

0

Uh?

This discussion is preserved as it was. New answers, comments, and votes are closed.