Self assigned as first arguement even when using colon

I’m trying to activate a function in a module by connecting to when the player hits an input. When I trigger InterpretInput, self prints as this through the module:

["InterpretInput"] = "function",
["__index"] = "*** cycle table reference detected ***"

this isnt what im looking for and isnt the table I assigned to self.

Code:

LocalScript:

UIS.InputBegan:Connect(function(input,gpEvent)
	if input.KeyCode==Enum.KeyCode.E then
		bookConstructor:InterpretInput(input,gpEvent)
	end
end)

Module:

function module:InterpretInput(input,gpEvent)
	print(self)
end

any help would be superb!

How do you make the variable bookConstructor? Like the function for the returned table.

Also just to make sure self is understood, self just is always the table that the function was called on. So in this case it’s going to be bookConstructor no matter if you overwrote a variable called self elsewhere.

I should probably add this too:

Where self is assigned:

function module.CreateBookTables(pages,author)	
	--PAGE CONSTRUCT
	local pageTable = {}
	for i=1,pages do
		pageTable[i]={
			Display = workspace:WaitForChild("Page1").SurfaceGui.TextBox;
			Text = "";
			Page = i;
		}
	end
	
	--DATA CONSTRUCT
	local dataTable = {
		LastInput = Enum.KeyCode.Unknown;
		LastInputTick = tick();
		BoldToggle = false;
		ItalicToggle = false;
	}
	
	--BOOK CONSTRUCT
	local bookTable = {
		PageTable = pageTable;
		DataTable = dataTable;
		Author = author;
		Title = "";
	}
	
	setmetatable(bookTable,module)
	return bookTable
end
1 Like

local bookConstructor = require(script.BookConstructor) in a local script (same script shown so far)

This probably isnt exactly how it works, but its something along the lines.

Basically any function that is in a table sets self to the table the parent is in.
Im not sure if there is a work around for it, so in the meantime dont use self

You need to make a local book = bookConstructor.CreateBookTables(...)
Then call the function book:InterpretInput(...)

The way you are currently using it is that you are using the function table instead of getting a data table.

1 Like

You’re invoking a method of the class on the class itself, not an instance of the class. Make sure you’re calling “InterpretInput” on what’s returned by module.CreateBookTables. To help clarify the problem:

-- What you should be doing
local instance = Class.new()
instance:Method()

-- Vs.

-- What you are doing:
local instance = Class.new()
Class:Method()

Thank you all. I suffer from the stupids and put the module instead of the table. :face_with_spiral_eyes:

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