local part = workspace.Part
while task.wait() do
print(part)
end
The part still exists in the script, even thought it has been deleted by a different script, anyone know why this is? (It prints “Part”)
local part = workspace.Part
while task.wait() do
print(part)
end
The part still exists in the script, even thought it has been deleted by a different script, anyone know why this is? (It prints “Part”)
You can use FindFirstChild
to check if the part is still in workspace. Something like this
local part = workspace.Part
while task.wait() do
if workspace:FindFirstChild(part.Name) then
print(part)
end
end
If this doesn’t work, let me see the script your using to delete the part, where its located and what type of script is it.
It is printing your definition of the part maybe…
local part = workspace:WaitForChild("Part")
task.wait(1)
part:Destroy()
local part = workspace:WaitForChild("Part")
task.wait(3)
while task.wait() do
print(part)
end
Would still print part, even if it isn’t there anymore.
check the part before printing:
local part = workspace.Part
while task.wait() do
if part then
print(part)
end
end
That’s not my goal. I want to know how i can make the reference nil if the part is deleted
The code checks if the part is nil or not, And if it is nil the script will automatically make the reference as nil.
The problem is the code says part exists, but it doesn’t.
You have to manually remove all refrences to deleted instances
while task.wait() do
if not part or not part.Parent then
part = nil
break
end
end
You need to manually set the variable to nil, just destroying the part isn’t enough. You could do this by using the Destroying
event
part.Destroying:Connect(function()
part = nil
end)
But can you say why you actually need the variable to be nil? There might be a better solution more specific to your problem
Destroying something sets and locks the parent to nil, but references to the part still held in your code will still be valid (but try printing the parent, should be nil).
The part won’t disappear completely until no references to it are held in code allowing it to be garbage collected.
So, you can check the workspace for the part, but if it has been reparented to anywhere else it may return nil but the part still exists in workspace.
Safer, would be to connect an event to set your reference to nil when it is being destroyed.
local part = workspace.Part
part.Destroying:Connect(function()
part = nil
end)
while task.wait() do
print(part)
end
while task.wait() do
if not part or not part.Parent then
part = nil
break
end
print(part)
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.