Object Attributes Not Showing?

function object.new(func: param1)
	local self = setmetatable(entity.new(script.Name), object)
	
	self.func = func
	
	return self
end

In this function, func is being defined in the self table. When accessed externally or even in the function itself, e.g.

self.func = func

--trying to access func again
self.func --no autofill existent 

it just seems to not exist at all. Can anybody assist me in this problem?

I think for this instance you’d have to define a type if you want it to work with autofill

type object<T> = typeof(setmetatable(entity.new(script.Name), object)) & {
	func: T
}

function object.new<T>(func: T): object<T>
	local self = {}
	
	self.func = func
	
	return self
end

Thanks! How would I work with this for multiple parameters? Also, will creating a new entity in the type mess with mem?

Apologies for my late reply,

If you know what type the other parameters will be beforehand, you don’t have to use generics (<T>) and you can set up as many as you want. Also creating a new entity in the type doesn’t actually create a new entity, the Luau interpreter only checks the return type of the entity so it shouldn’t mess with the mem.

type object = typeof(setmetatable(entity.new(script.Name), object)) & {
	func: () -> (),
	property0: number,
	property1: string
}

function object.new(func: () -> ()): object
	local self: object = {}

	self.func = func
	self.property0 = 0
	self.property1 = "test"

	return self
end

1 Like

So just double checking, setmetatable won’t actually do anything with memory, and it’s just there for type checking?

1 Like

Yes exactly, it’s only for the type checking

1 Like

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