When you use :Clone() on the part Instance, you are creating a non-parented new Instance. When you proceed to delete the original Instance, the non-parented copy still remains as it holds a reference. It is independent of the part it’s cloned from and resides in nil.
I think he’s referring to this situation, which weirdly works (I guess the Instance reference is still in memory…? Instances are sent as identifiers, so that’d be the only way…).
EDIT: Yes, that is why, and it probably doesn’t work outside Studio. This sends nil as expected.
On the server, before sending to the other clients.
One Client:
RE:FireServer("BreakGlass", {Glass=Hit})
Server Event
if Type == "BreakGlass" then
Args.Glass:Destroy()
RE:FireAllClients("ClientGlassEffect",Args)
end
All Clients Events
if Type == "ClientGlassEffect" then
local Clone = Args.Glass:clone()
--do effect stuff
end
tbh i thought the behavior would be like how you described. i’m surprised it truned out like this too
Have you tested this in a real server yet?
Yes, it works too.
That’s strange and I wouldn’t rely on this behavior.
Instead of cloning the reference after its destroyed, is it fine to just read the properties of the destroyed part?
Size, CFrame,etc.?
I’d instead tag all glass something with CollectionService beforehand, destroy it on the server, then use GetInstanceRemovedSignal to know when glass was destroyed. From there, you can clone and do your effects. This way, it replicates with the least amount of memory and you don’t even need a RemoteEvent.
All of this ties back to my original question.
So we’ve established that the instance reference is still in memory, since it’s sent as an identifier.
We are able to clone the instance, read from it. The only thing we can’t do is re-parent it somewhere.
So when is this reference cleared from memory? When is the cloned glass reference cleared from memory? I assume after the glass effect procedure is done?
When you lose all references to it. In the case of my example, when the callback for GetInstanceRemovedSignal ends.
When it too is Destroyed and all references to it are dropped.
I’m sorry, can you be more specific about “dropping” or “losing” references to it? Is this a more manual method that requires more coding or …?
In the case of Lua, they’re synonymous, I’m just unintentionally being confusing.
function proc(something)
-- we have a reference to something, so it is NOT garbage collected
-- bla bla bla do stuff with something
end
local list = {} -- 1 reference to the data for "list"
proc(something) -- proc references "list" too, so 2 things reference the data for "list"
-- when proc finishes, it no longer needs a reference to the data under "list", so there's only 1 reference left
list = nil
-- there are now no more references to the data that used to be under "list", so it will be GC'd
Lua isn’t a systems language or anything, so to get rid of a reference, it simply needs to go out of scope.
so in my case im not using tables or anything, just simple variable indenitifer
would the same logic work here? or would i need to do something like
glassclone = nil
or
glass = nil
You don’t need to manually set it to nil. Once there’s no more references to it, it gets GC’d.
local function doEffects(glass)
local glassClone = glass:Clone()
-- effects code
glassClone:Destroy()
end
Once doEffects
finishes, glassClone
is GC’d because it’s parented to nil and nothing left references it.
thanks