compareinstances
Check if two Instance references point to the same underlying object.
function compareinstances(object1: Instance, object2: Instance): booleanSynopsis
How it works
Compares the underlying C++
Instance* pointers behind two userdata wrappers. Returns true if both wrappers point to the same Roblox engine Instance, regardless of whether the Lua references are the same object. This is the tool for verifying identity after cloneref().Why rawequal fails
Each
cloneref() call allocates a new userdata in Luau's heap. Lua's rawequal compares userdata addresses, so two different allocations are never equal. compareinstances dereferences the userdata to get the C++ pointer and compares at that level.Usage
Equality with cloneref
print(compareinstances(game, game)) "cc">-- true
print(compareinstances(game, workspace)) "cc">-- false
print(compareinstances(game, cloneref(game))) "cc">-- true
print(game == cloneref(game)) "cc">-- falseParameters
object1 Instance The first Instance to compare.
object2 Instance The second Instance to compare against.
Returns
boolean true if both references point to the same underlying game Instance.
Required with cloneref
Whenever you use cloneref, use compareinstances instead of == for reliable equality checks.