Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to wait for an object to be removed?

Asked by 6 years ago

While working on an ongoing project, I ran into the dilemma of having to run two remote functions right after the other. I planned on using the wait function in order to wait for the events to finish, however it will only wait for the event to fire.

To solve this, I tried waiting for an object that would be copied from the main server to the PlayerGui on the ServerScript, however, I haven't found the correct way of waiting for the object to be removed.

function OnClicked()
    game.ServerScriptService.MainGui.ChatNote:InvokeServer("Num1","Text1")
    -- Asks the server to set a Gui title to "Num1" and text to "Text1"

    local noted = script.Parent.Parent.Parent:WaitForChild("ChatNote") 
    -- Waits till "ChatNote" is added

    wait(noted == nil)
    -- Does not wait

    game.ServerScriptService.MainGui.ChatNote:InvokeServer("Num2","Text2")  
    -- Same as line 2
end
script.Parent.MouseButton1Down:connect(OnClicked)

Any comments are appreciated and thank you.

0
This is by no means the optimal solution but you can run a repeat loop. repeat wait() until notel==nil lukeb50 631 — 6y

2 answers

Log in to vote
1
Answered by
Validark 1580 Snack Break Moderation Voter
6 years ago

Corecii wrote us a nice module that can solve this problem called InstanceDestroyed. It runs a function when an instance is destroyed. How nice!

local UponInstanceDestroyed = require(483039669)
-- This returns a function with parameters Instance and Func
-- Where Instance is the Object you are waiting to be Destroy()ed
-- And Func is the function you want to run when it is Destroy()ed

UponInstanceDestroyed(workspace.Folder, function(Object)
    print(Object:GetFullName(), "was destroyed! How pathetic it must be!")
end)

Note: Roblox Studio's delete function is not the same as the Destroy method

Ad
Log in to vote
0
Answered by 6 years ago

As suggested by @lukeb50, using a repeat until loop currently solves my problem waiting for the object to be removed.

--Current solution

local noted = script.Parent.Parent.Parent:WaitForChild("ChatNote")
-- Waits to see if ChatNote exists

repeat wait() until script.Parent.Parent.Parent:FindFirstChild("ChatNote") == nil
-- Waits until the ChatNote does not exist 

Further testing will be conducted later on if this causes any problems down the road, thank you @lukeb50 for helping me solve my problem.

Answer this question