Hello people, quick help here

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

No, when as in if part got destroyed = rest of the script here

1 Like

have you seen my code post above?

1 Like

ok you have two options, yield until Destroyed is fired or connect the event

1 Like

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
1 Like

Do i put this into the part which is going to be destroyed?

1 Like

Part.Destroying:Connect(YourFunction)

2 Likes

That’s what my, @V_ladzec, and @MeltLava800 code does, I would advise @V_ladzec’s code tho

1 Like