WaitFor
Utility class for awaiting the existence of instances.
By default, all promises timeout after 60 seconds, unless the timeout
argument is specified.
note
Promises will be rejected if the parent (or any ancestor) is unparented from the game.
Set name before parent
When waiting for instances based on name (e.g. WaitFor.Child
), the WaitFor
system is listening to events to capture these instances being added. This
means that the name must be set before being parented into the object.
Properties
Error
WaitFor.Error:
{
Unparented:
string
,
ParentChanged:
string
}
Functions
Child
Wait for a child to exist within a given parent based on the child name.
WaitFor.Child(parent, "SomeObject"):andThen(function(someObject)
print(someObject, "now exists")
end):catch(warn)
Children
Wait for all children to exist within the given parent.
WaitFor.Children(parent, {"SomeObject01", "SomeObject02"}):andThen(function(children)
local someObject01, someObject02 = table.unpack(children)
end)
note
Once all children are found, a second check is made to ensure that all children
are still directly parented to the given parent
(since one child's parent
might have changed before another child was found). A rejected promise with the
WaitFor.Error.ParentChanged
error will be thrown if any parents of the children
no longer match the given parent
.
Descendant
Wait for a descendant to exist within a given parent. This is similar to
WaitFor.Child
, except it looks for all descendants instead of immediate
children.
WaitFor.Descendant(parent, "SomeDescendant"):andThen(function(someDescendant)
print("SomeDescendant now exists")
end)
Descendants
Wait for all descendants to exist within a given parent.
WaitFor.Descendants(parent, {"SomeDescendant01", "SomeDescendant02"}):andThen(function(descendants)
local someDescendant01, someDescendant02 = table.unpack(descendants)
end)
note
Once all descendants are found, a second check is made to ensure that none of the
instances have moved outside of the parent (since one instance might change before
another instance is found). A rejected promise with the WaitFor.Error.ParentChanged
error will be thrown if any of the instances are no longer descendants of the given
parent
.
PrimaryPart
Wait for the PrimaryPart of a model to exist.
WaitFor.PrimaryPart(model):andThen(function(primaryPart)
print(primaryPart == model.PrimaryPart)
end)
ObjectValue
Wait for the Value of an ObjectValue to exist.
WaitFor.ObjectValue(someObjectValue):andThen(function(value)
print("someObjectValue's value is", value)
end)
Custom
WaitFor.
Custom
(
predicate:
(
)
→
T?
,
timeout:
number?
) →
Promise
<
T
>
Wait for the given predicate function to return a non-nil value of
of type T
. The predicate is fired every RunService Heartbeat step.
-- Example, waiting for some property to be set:
WaitFor.Custom(function() return vectorForce.Attachment0 end):andThen(function(a0)
print(a0)
end)