'Attempt to call missing method 'UpdateViewmodel' of table' error with OOP

Hi,

As the title mentions, the instance (viewmodel_instance) of the class is not able to call a specific method on itself. My guess is that I did a mistake in setting up the constructor (but even after reviewing my code multiple times I can’t see the issue) since the metatable (which is also the class itself) prints the method with no issue.

Output:

table:   ▼  {
                    ["_instance"] = Viewmodel_woitur,
                    ["_name"] = "woitur"
                 } 

 metatable:   ▼  {
                    ["UpdateViewmodel"] = "function",
                    ["_index"] = "*** cycle table reference detected ***",
                    ["new"] = "function"
                 }  

ERROR: Players.woitur.PlayerScripts.LocalScript:49: attempt to call missing method 'UpdateViewmodel' of table

(Simplified) Local Script:

local viewmodel_instance; --holds the viewmodel object

--Runs code even if character is added before characteradded event can be fired
local function onCharacterAdded(character: Model): ()
	print("Character Added Ran")
	
	if not viewmodel_instance then
		viewmodel_instance = ViewmodelModule.new(character);
	else
		print("Viewmodel for that character already exists, did not create a new one");
	end
end

LocalPlayer.CharacterAdded:Connect(onCharacterAdded)

if LocalPlayer.Character then
	onCharacterAdded(LocalPlayer.Character);
end

game:GetService("RunService").RenderStepped:Connect(function() --Here I printed the output
	print("table: ",viewmodel_instance);
	print("metatable: ",getmetatable(viewmodel_instance))
	viewmodel_instance:UpdateViewmodel();
end);

(Simplified) module itself:

local ViewmodelModule = {};
ViewmodelModule._index = ViewmodelModule;

function ViewmodelModule.new(character: Model)
	
	local function SetUpViewmodel()
		
		local viewmodel_template = workspace:FindFirstChild("Viewmodel_Template");
		local viewmodel = viewmodel_template:Clone();
		viewmodel.Name = ("Viewmodel_"..character.Name);
		
		
		local left_arm = viewmodel:FindFirstChild("LeftArm");
		local right_arm = viewmodel:FindFirstChild("RightArm");
		
		local body_colors = character:FindFirstChildWhichIsA("BodyColors");
		
		if body_colors then
			left_arm.Color = body_colors.LeftArmColor3;
			right_arm.Color = body_colors.RightArmColor3;
		else
			error("Body Colors not found/loaded");
		end
		
		viewmodel.Parent = workspace
		
		return viewmodel; --its the physical instance (local variable in the function)
	end
	
	local viewmodel_instance = setmetatable({
		_name = character.Name;
		_instance = SetUpViewmodel();

	}, ViewmodelModule);
	
	return viewmodel_instance;
end

function ViewmodelModule:UpdateViewmodel()
	self._instance:PivotTo(Camera.CFrame);
end

Thank you for your time

2 Likes

The index metamethod needs to be prepended by two underscores, not one (__index vs. _index). This also goes for all metamethods

2 Likes

:man_facepalming:

I have no idea how I did not see it

Thank you

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.