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.
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
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