"self" is returning functions, not properties of constructor function

For some reason whenever I print “self”, it prints out the list of functions that are associated with the object and not the properties of the object. Here’s my constructor function:

function ViewModel.new()
	local self = setmetatable({}, ViewModel)
	
	self.Model = script.ViewModel:Clone()
	self.Model.Parent = workspace
	
	local Animator = Instance.new("Animator")
	Animator.Parent = self.Model.AnimationController
	
	return self
end

Function that prints out “self”:

function ViewModel:RenderSteppedUpdate(dt)
	print(self) -- Prints a list of functions
end

If you’re just calling ViewModel:RenderSteppedUpdate to test then it’s going to call at a class-level and naturally would print class-level methods and properties. Make an object of the class and call the same method, you will get different results.

Repro code:

local ViewModel = {}
ViewModel.__index = ViewModel

function ViewModel.new()
	local self = setmetatable({}, ViewModel)
	
	self.Model = "Yeah"

	return self
end

function ViewModel:RenderSteppedUpdate(prefix)
	print(prefix, self) -- Prints a list of functions
end

local foobar = ViewModel.new()
ViewModel:RenderSteppedUpdate("Class-level")
foobar:RenderSteppedUpdate("Object-level")

image

2 Likes