I’ve been trying to assemble a script for this, but no luck for me.
I want to create a loop that stops when it realised that the part it’s waiting for has gotten destroyed.
After it got destroyed, it does the action mentioned in the script.
What does this part look like and where is it (as in what is its parent)? There are multiple ways to go about this.
That isn’t exactly what I am asking. This part is waiting for that part to get Destroyed, and once it’s destroyed, it completes [x] action.
How it looks like and where is is doesn’t matter, I believe.
It mattres on :Destroying and something else.
Could you share the code you have so far, so we can better assist and guide you?
while wait() do
local part = workspace:WaitForChild("Baseplate")
print("e")
if not part then
break
end
end
Something like this? the loop stops once the baseplate is destroyed
Then make a function, and put everything that has to run inside that function. Then add a ChildRemoved
event to the part’s parent, and check if the part that has been removed is the part you’re waiting for. If it is, run the function.
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