How do i change :Destroy function in all objects?

i want to override the :Destroy function, to count how much objects were destroyed(note, i cant use .Destroying or something like that)
Im making a lua sandboxer and I need to count how much times :Destroy was called in script, and if its more then do something if not then do smth else(i know how to use if else chat :pray:)
I hope anyone knows :sob:

You can’t really overwrite a method; However, you could use a helper function and just call that function instead of using :Destroy() directly:

function Destroy(root: Instance): number
   local numDescendants = #root:GetDescendants()
   rootInstance:Destroy()
   return numDescendants + 1
end

I am making a Lua sandboxer, and due to me not being able to modify the code im sandboxing your method isnt applicable :sob:

count how many objects there were before and after, and do: before - after

as it as already been said; there is no way to overwrite an instance’s method. Even if there were, it’d likely fall under “misusing Roblox systems” by exploiting a vulnerability in Roblox’s engine code.

However, similar to what East98 said (no ping) - you could create some kind of middleground which acts as a proxy to the instance.

function yourInstanceFuncs.new(className: string)
    local inst = Instance.new(className)

    --now you can create a proxy to forward operations to the instance
    local proxy = newproxy(true)
    local mt = getmetatable(proxy)

    --add metamethods to interact with the proxy
    mt.__newindex = function(_, k, v) inst[k] = v end
    mt.__call = function() return inst() end --to recreate actual errors and stack traces
    --continue to add metamethods, et cetera

    --now, add __index
    --Lua userdata objects cant have __namecall but we can still detect with __index
    mt.__index = function(_, k)
        local val = inst[k]

        if (type(val) == "function" and k:lower() == "destroy") then --detect deprecated :destroy() too
            --your custom logic for Destroy
            inst:Destroy()
        end
    end

    --return the proxy
    return proxy
end

you could maybe make it return a function for your custom destroy so you can use the parameters.

2 Likes

How about counting the number of times an instance is removed in the game instead?
Example:

game.DescendantRemoving:Connect(function(instance)
	if instance.Parent == nil then
		if instance:IsA("Instance") then
			print("removing")
		end
	end
end)

Might be late, but you can just make a number value somewhere and add to that value every time something is :Destroyed()

much like

local NumberValue = game.Workspace.NumberValue

--- your script
Part:Destroy() -- Destroy part
NumberValue.Value += 1 -- add to the number value

This is a server sided method, can also work with client sided. Real simple and easy imo.

i want to prevent execution of destroy after certain cap. Though yeah ig

YO, thats EXACTLY what i needed! Thank you. i will modify this a bit tho. Thank you!

1 Like