local NPC = {}
NPC.__index = NPC
function NPC.new(name:string)
local NewNPC = {
Name = name,
}
return setmetatable(NewNPC, NPC)
end
-- other functions ...
return NPC
local NpcClass = require(game.ServerScriptService.NpcClass)
local npc = NpcClass.new("Goblin")
How can I delete npc ?
I tried to do npc = nil but the functions in the class still working
As long as there isn’t a reference to npc, it will be garbage collected, aka deleted. However, if there is something like an Instance in the class, you will need to manually destroy it.
You can add a :Destroy() method into the Class. The best thing you can do inside of it is to remove the metatable and clear the table. Destroy Instances and Disconnect Connections as well if you have any too.
local NPC = {}
NPC.__index = NPC
-- Constructor and other methods...
-- Ensure to add a `:Destroy()` method whenever possible!
function NPC:Destroy()
setmetatable(self, nil)
table.clear(self)
table.freeze(self)
end
return NPC