Perfect! This is great. But i mentioned it needs to start it’s actions after part is destroyed.
You could try using a childremoved event, it should work, IIRC it should be like:
local part = --your part here
local function onChildRemoved(child)
if child = part then
while Wait() do
--do something
end
end
end
workspace.ChildRemoved:Connect(onChildRemoved)
Instance.Destroying:Connect(function()
-- your code here
end)
The WaitForChild
wouldn’t work here. It yields the code until the part appears. If the part is removed, it’s just going to wait forever without doing anything. FindFirstChild
might work but it’d rather not use a loop for something like this, since it can be done with events. Also, if there are multiple parts with the same name, this will fail. Events are more reliable and also have a smaller impact on performance than a loop that runs every frame.
Also, wait()
is deprecated, use task.wait()
.
Not really, if you need throttling wait() is right
wait()
is deprecated. Yes, it still works, but task.wait
is faster and more reliable.
part.Destroyed:Wait()
this makes the script yield until the Destroyed
event has fired
you can also do part.Destroyed:Connect(function()
and then end)
to run the anon function inside :Connect()
once Destroyed
has been fired
remember both are different things, :Wait()
yields the thread it’s contained in and :Connect()
runs the function when the event is fired
Okay, but in case we need throttling? Throttling is still useful
This is also not what is required. You’re doing while wait for part to destroy then do action.
I need when part destroyed do action.
i see, use :Connect()
if you don’t want to yield
It does do what it does, if the child destroyed is your part it’ll run the loop, if it isn’t it won’t
Okayokayokay, I think I explained it badly.
Loop’s purpose = find out when the part is destroyed
not do something after the part is destroyed
i see, so if you want to store the time use :Connect()
then store some time variable with the date,unix timestamp, iunno
No, when as in if part got destroyed = rest of the script here
have you seen my code post above?
ok you have two options, yield until Destroyed
is fired or connect the event
You don’t need that tho, it’s just a waste of time, and possibly a memory leak, but because you asked…
local part = part
while Wait() do
if workspace:FindFirstChild(part) == nil then
--do stuff
end
end
Do i put this into the part which is going to be destroyed?
Part.Destroying:Connect(YourFunction)