Hello people, quick help here

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.

6 Likes

What does this part look like and where is it (as in what is its parent)? There are multiple ways to go about this.

3 Likes

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.

3 Likes

How it looks like and where is is doesn’t matter, I believe.

It mattres on :Destroying and something else.

3 Likes

Could you share the code you have so far, so we can better assist and guide you?

3 Likes
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

4 Likes

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.

3 Likes

Perfect! This is great. But i mentioned it needs to start it’s actions after part is destroyed.

3 Likes

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)
3 Likes
Instance.Destroying:Connect(function()
    -- your code here
end)
3 Likes

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().

5 Likes

Not really, if you need throttling wait() is right

3 Likes

wait() is deprecated. Yes, it still works, but task.wait is faster and more reliable.

2 Likes

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

1 Like

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.

1 Like

i see, use :Connect() if you don’t want to yield

1 Like

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

1 Like

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

1 Like

i see, so if you want to store the time use :Connect() then store some time variable with the date,unix timestamp, iunno

1 Like