InstanceUtil
A collection of utility functions for working with Instances.
Types
AnimPlayInfo
type
AnimPlayInfo =
{
}
FadeInTime: number?,
Weight: number?,
Speed: number?,
FadeOutTime: number?,
} A table of info for generating tweens for playing animations.
Functions
findFirstChildFromPredicate
InstanceUtil.
findFirstChildFromPredicate
(
predicate:
(
child:
Instance
)
→
boolean
,
--
The predicate which determines whether the child was found.
recurse:
boolean?
--
Whether or not to search the parent's descendants instead of just its children.
) →
Instance?
--
The first child whose name matches the given string.
Searches the parent for the first child which evaluates the given predicate to be true.
findFirstChildThatMatches
InstanceUtil.
findFirstChildThatMatches
(
matchString:
string
,
--
The string to match the child's name to. Uses Lua's string.match function. Can take patterns.
recurse:
boolean?
--
Whether or not to search the parent's descendants instead of just its children.
) →
Instance
--
The first child whose name matches the given string.
Searches the parent for the first child whose name matches the given string.
findFirstChildOfAncestor
InstanceUtil.
findFirstChildOfAncestor
(
) →
Instance
--
The first child of the ancestor that is an ancestor of the descendant.
Finds the first child of the given ancestor that is an ancestor of the given descendant. This is useful when you have a bunch of models in a folder and you have a reference to a part in one of these models. It allows you to quickly find which of those immediate children models the part is in.
getDescendantsWhichIsA
InstanceUtil.
getDescendantsWhichIsA
(
ClassName:
string
|
{
string
}
--
The class name or names the descendant must match or inherit.
) →
{
Instance?
}
--
Table with valid descendants of passed ClassName.
Iterates through Parent descendants and returns a table which only contains descendants of passed ClassName.
waitForChildFromPredicate
InstanceUtil.
waitForChildFromPredicate
(
predicate:
(
child:
Instance
)
→
boolean
,
--
The predicate which determines whether the child was found.
timeout:
number?
,
--
The maximum amount of time to wait for the child to be added. Defaults to 10 seconds.
recurse:
boolean?
--
Whether or not to search the parent's descendants instead of just its children.
) →
Promise
<
Instance
>
--
A Promise resolving with the first child who satisfies the predicate.
Waits for the first child which evaluates the given predicate to be true.
waitForChildWhichIsA
InstanceUtil.
waitForChildWhichIsA
(
className:
string
,
--
The class of the child to wait for.
timeout?:
number?
--
The maximum amount of time to wait for the child to be added. Defaults to 10 seconds.
) →
Promise
<
Instance
>
--
A promise that resolves with the child when it is added.
Waits for a child of the given class in the given ancestor to be added.
waitForChildThatMatches
InstanceUtil.
waitForChildThatMatches
(
matchString:
string
,
--
The string to match the child's name to. Uses Lua's string.match function. Can take patterns.
timeout:
number?
,
--
The maximum amount of time to wait for the child to be added. Defaults ot 10 seconds.
recurse:
boolean?
--
Whether or not to search the parent's descendants instead of just its children.
) →
Promise
<
Instance
>
--
The first child whose name matches the given string.
Waits for the first child whose name matches the given string.
ensureInstance
InstanceUtil.
ensureInstance
(
name:
string?
--
The name of the child to find. Uses the template's name if not given.
) →
Instance
--
The existing or new child.
Ensures that the given parent has a child with the given name. If not then it uses the given template to create a new child.
destroyFirstChild
InstanceUtil.
destroyFirstChild
(
descendantName:
string
,
--
The name of the descendant to destroy.
recurse:
boolean?
--
Whether or not to search the parent's descendants instead of just its children.
) →
(
)
Attempts to destroy a named descendant of the given parent.
safeDestroy
InstanceUtil.
safeDestroy
(
) →
(
boolean
,
--
Whether or not the instance was destroyed. False is the instance was already destroyed.
string
--
The error message if the instance could not be destroyed.
)
Attempts to destroy the given instance.
ensureAnimator
Ensures the getting and creation of an Animator in the given Parent.
loadAnimAsync
Loads an animation asynchronously. Originally made to be used with models being displayed in ViewportFrames with Fusion.
weld
Creates a WeldConstraint between two parts.
weldAssembly
Weld each individual part to the Model Model .PrimaryPart;
getAttachmentsAlignedCFrame
Gets the CFrame of the given partAttachment
's parent needed to align with the targetAttachment
.
This is useful for aligning two parts such that their attachments are equivalent in CFrame.
info
This is effectively the same as using a RigidConstraint with the attachments if the parent part is unanchored.
getModelFitDistance
InstanceUtil.
getModelFitDistance
(
) →
number
--
The distance from the model the camera should be.
Gets the distance from the camera to the model that would fit the model in the viewport frame.
guaranteeAnchoringToAnimate
InstanceUtil.
guaranteeAnchoringToAnimate
(
) →
(
)
Iterates through descendants of Model and unanchor necessary parts to play animations correctly.
emitParticles
InstanceUtil.
emitParticles
(
emitCount:
number?
--
The number of particles to emit
) →
(
)
Takes an Instance and Emits it and any descendants it has.
Optional Attributes that any of the descendant ParticleEmitters could have:
EmitDelay: number? -- The delay before emitting
EmitCount: number? -- The number of particles to emit at the start
EmitDuration: number? -- The duration to emit particles for
cloneChildren
Takes an Instance and Clones all of its children into a new Instance.
promiseChild
InstanceUtil.
promiseChild
(
childName:
string
,
--
The Instance name to look for
timeout:
number?
--
The number of seconds to wait before timing out
) →
Promise
--
A Promise that resolves when the child is found or rejects if the timeout is reached.
Promisifys the WaitForChild method on an Instance and adds in more robust error handling.
isClass
InstanceUtil.
isClass
(
classNames:
string
|
{
string
}
--
The ClassName or ClassNames to check against.
) →
boolean
--
Whether or not the instance is any of the given classes.
Checks to see if the given instance is any of the given classes.
fetchModule
InstanceUtil.
fetchModule
(
moduleName:
string
,
--
The name of the ModuleScript to search for.
defaultValue:
any?
--
The default value to return if the ModuleScript could not be found.
) →
any
Searches for a ModuleScript in the given parent with the given name. If a descendant is found with the given name and is an ObjectValue, this value will be assumed to be the ModuleScript. If the ModuleScript could not be found it will return the defaultValue if it is provided. Otherwise it will error.
hasProperty
InstanceUtil.
hasProperty
(
property:
string
--
The property to check for.
) →
(
boolean
,
--
Whether or not the instance has the property.
any
--
The value of the property if it exists.
)
Checks to see if a given instance has a property. If it does, it will return true and the value of the property. If it does not, it will return false and a message.
playTracksAsync
InstanceUtil.
playTracksAsync
(
tracks:
AnimationTrack
|
{
AnimationTrack
}
,
--
The AnimationTrack or array of AnimationTracks to play.
keyframeMarkerToResolveAt:
string?
--
The Keyframe marker to resolve at instead of the animations ending
) →
Promise
--
A Promise that resolves when all tracks have stopped playing.
Takes a track or array of AnimationTracks and plays them all asynchronously.
playTween
InstanceUtil.
playTween
(
goals:
{
[
string
]
:
any
}
?
) →
Promise
--
A Promise that resolves when the tween has finished.
Plays a tween as a promise. If a tween is not given then standard tween parameters are used to create a new tween.