Anyone knows what's happening here?

Hello. I have programmed for a while using strict typing but something completely unexpected just happened. I have this Destroy method inside a class called Gun:

function Gun:Destroy()
	self = nil
end

Very simple, just sets self to nil.
I call the Destroy method when I unequip my physical tool:

GunInstance.Unequipped:Connect(function()
	GunObject:Destroy()
end)

Though, I get a warning from the type checker when calling the Destroy method.
image

From my point of view the content of this warning has nothing to do with the Destroy method because I never try to call a value of type number on the line I’m getting this warning.
Also, I know for a fact that the Gun object is being created correctly.

My doubt is, is this a mistake from the type checker? I literally have no idea how to solve it because the type checker warns something completely unrelated to my Destroy method.

(Keep in mind, I never reassign self to be a value of type number).

Update, used a dot instead of a colon to call it and it worked lol
(Still don’t know why the warning is completely unrelated)

I’m pretty sure you can’t manually flush a table like this. Lua implicitly declares self within the method’s scope, so setting it to nil only updates the local variable. The next best thing I can think of is:

function gun:destroy()
	for k, _ in self do
		self[k] = nil;
	end
end

This might help to minimize useless references and prime them for garbage collection, but it’s pretty lazy and probably prone to error depending on your use case.

2 Likes

Yeah, I’ll switch the cleanup eventually but I haven’t implemented any other method yet.

	table.clear(self)
	setmetatable(self, nil)
2 Likes