Tip: How to tell if ObjectValue.Value has been destroyed/is actually nil

Hi, I thought I’d share this to help anybody who had the same problem as me, which was that when a part that is a value of an ObjectValue is destroyed, ObjectValue.Value does not become nil.

So, as an example, if you run this code, it’ll print “part exists,” even though part has been destroyed

local part=Instance.new("Part")
part.Parent=game.Workspace

local objVal=Instance.new("ObjectValue")
objVal.Parent=game.Workspace

objVal.Value=part

part:Destroy()
if objVal.Value~= nil then
          print("part exists")
else
          print("part does not exist")
end

A solution is to, instead of checking if the value is nil, check if the value is a descendant of workspace (or whatever it should be a descendant of)

Here is the example, but using the solution:

local part=Instance.new("Part")
part.Parent=game.Workspace

local objVal=Instance.new("ObjectValue")
objVal.Parent=game.Workspace

objVal.Value=part

part:Destroy()
if objVal.Value:IsDescendantOf(game.Workspace) then
          print("part exists")
else
          print("part does not exist")
end

Anyway, I hope this saves somebody’s time. I know there are other solutions that will work in more situations (like if the part is moved from Workspace to ServerStorage), but this one still has it’s place.

7 Likes

I’ve bin trying to find a solution for this problem for so long thank you so much