#Is there any way of destroying OOP Objects like Instance?

1 messages · Page 1 of 1 (latest)

sudden schooner
#
local Thing = {}
Thing.__index = Thing
Thing.new = function()
return setmetatable({}, Thing)
end
return Thing

function:

function Thing:Destroy()
setmetatable(self, nil)
table.clear(self)
self = nil — it wont mske it nil in different scripts
end

is there any way of erasing an object like on every single script it requires it would get nil ?

restive impBOT
#

studio** You are now Level 2! **studio

autumn dawn
formal arch
#

the best thing you could do is setting the variables in _G to nil

pulsar ice
#

yeah dont use _G

pulsar ice
#

Updated Code:

local Thing = {}
Thing.__index = Thing

local allThings = {}
local listeners = {}

local function UpdateAll()
    for _, callback in listeners do 
        callback()
    end
end

function Thing.New()
    local self = setmetatable({}, Thing)
    table.insert(allThings, self)

    UpdateAll()

    return self
end

function Thing:Destroy()
    for i, v in ipairs(allThings) do
        if v == self then
            table.remove(allThings, i)
            break
        end
    end

    UpdateAll()
end

function Thing.Changed(callback)
    table.insert(listeners, callback)
end

function Thing.All()
    return allThings
end

return Thing

Example Usage:

local Thing = require(game.ServerScriptService.Rojo.libs.firerecieve)

local all = Thing.All()

Thing.Changed(function()
    all = Thing.All()   
end)

print(all)

local thing = Thing.New()

print(all)

thing:Destroy()

print(all)

What do you think of this @sudden schooner

#
local Thing = require(game.ServerScriptService.Rojo.libs.firerecieve)

local all = Thing.All()

Thing.Changed(function()
    all = Thing.All()   
end)

you'd need these in all scripts tho

sudden schooner
#

holy fuck id better set thing to just an empty table

pulsar ice
#

?