How can I destroy a part and all references to it?

To specify my problem even more. I want object values to go nil when the part in the object value is :Destroyed() but that doesn’t happen so if I really wanted to make the object value go nil when the part is destroyed I would have to have a loop constantly check.

How can I destroy all references to a part?

I do not believe there is a way to have ObjectValue’s value automatically set to nothing after a part is destroyed.

The only way I think about doing so, is this:

for i,v in pairs(game.Workspace:GetDescendants()) do
    if v:IsA("ObjectValue") then
        v.Value.AncestryChanged:Connect(function()
            if not v.Value:IsDescendantOf(game) then
                v.Value = nil
            end
        end)
    end
end
1 Like
local Game = game

local function OnDestroying(Value, ObjectValue)
	if Value ~= ObjectValue.Value then return end
	ObjectValue.Value = nil
end

local function OnValueChanged(Value, ObjectValue)
	if not Value then return end
	Value.Destroying:Connect(function() OnDestroying(Value, ObjectValue) end)
end

local function OnGameDescendantAdded(Descendant)
	if not (Descendant:IsA("ObjectValue")) then return end
	Descendant.Changed:Connect(function(Value) OnValueChanged(Value, Descendant) end)
	OnValueChanged(Descendant.Value, Descendant)
end

Game.DescendantAdded:Connect(OnGameDescendantAdded)
for _, Child in ipairs(Game:GetDescendants()) do
	pcall(function()
		for _, Descendant in ipairs(Child:GetDescendants()) do
			OnGameDescendantAdded(Descendant)
		end
	end)
end

messy code aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa