This would actually cause a lot of major issues.
Hereâs the first part that you need to understand - Instances are just userdata
s with metamethods that proxy calls to the underlying C++ API. Theyâre created on the C side, and metatables written, then they are exposed to the Luau side. This is what you reference in scripts. For example, calling Instance:GetFullName()
triggers __namecall
, which calls, and returns the values from, the corresponding function in C++.
The next part - Luaâs nil
. This is not truely nothing; the same way NULL
in C++ is typically just 0. nil
is Luaâs way of representing the absence of data, but it is still data in itself. The closest you will ever get to nothing is C++'s nullptr
, which points to memory location 0x0
(followed by however many 0s⌠basically just 0). Itâs important to note that trying to reference nullptr
as data leads to undefined behaviour, and typically causes a segmentation fault. This is when code tries to access invalid memory, i.e. itâs been cleaned up. Segmentation faults usually cause a crash - note this.
Luau closures (functions with an environment) capture upvalues. These are values which the function references within itâs code. They are pushed to and popped from the Luau stack, along with their closure, and are accessible from both C++ and Luau. If an instance and itâs corresponding userdata
get cleaned up, i.e. freed from memory, that Luau upvalue now points to nothing, which Iâm not even sure Luau allows support for. This would result in numerous seg faults as various sections of C++ and Luau code now references nullptr
, thinking itâs still valid data - resulting in crashes.
Even if all references got set to nil
immediately, how would your code know? Youâd need to add an absolute tonne of if instance then
checks all over the place, and if you donât itâd error. Not to mention, this would take a lot more memory as all references would need to be tracked.
Luau is a memory-safe language, meaning that memory gets automatically cleaned - this is garbage collection. When a value is no longer referenced anywhere, only then will it be freed from memory. This removes the danger of segmentation faults and results in safe accessing of memory.
Hope this helps, any questions please ask!
TL;DR: would cause crashes and other issues (i cant really TLDR it more than that xd)