instanceAttributeUtil
A utility module for working with instance attributes.
Functions
all
Returns a read only array of all descendants in instanceTree
which have the
targetAttribute
attribute.
observeMany
instanceAttributeUtil.
observeMany
(
attributesToObserve:
{
string
}
,
observer:
(
newAttributes:
{
[
string
]
:
any
}
,
oldAttributeValue:
{
[
string
]
:
any
}
?
)
→
(
)
) →
RBXScriptConnection
Works similar to instanceAttributeUtil.observe, but observes an array of attributes instead of a single attribute.
instanceAttributeUtil.observeMany(workspace.Part, {"Number1", "Number2"}, function(newAttributes, oldAttributes)
print(newAttributes.Number1 + newAttributes.Number2)
end)
observeDescendantAdding
Sets up an event connection which listens to any descendant added to instanceTree
, if it has the
targetAttribute
atribute, then targetAttribute
will be called.
This method will also capture the initial descendants of the given instanceTree
as well.
instanceAttributeUtil.observeDescendantAdding(workspace, "Test", function(instance)
print(instance)
end)
observeDescendantRemoving
Sets up an event connection which listens to any descendant removed from instanceTree
, if it has the
targetAttribute
atribute, then callback
will be called. Returns a RBXScriptConnection object.
instanceAttributeUtil.observeDescendantRemoving(workspace, "Test", function(instance)
print(instance)
end)
observe
instanceAttributeUtil.
observe
(
targetAttribute:
string
,
observer:
(
newAttributeValue:
any
,
oldAttributeValue:
any
)
→
(
)
) →
RBXScriptConnection
Observes the value of the attribute targetAttribute
in instance
. observer
will initially be immediately called if targetAttribute
exists in instance
, being passed the attribute value as the only argument (newAttributeValue
), whereas oldAttributeValue
will be nil
during this time. Additionally from this point onwards, everytime targetAttribute
in instance
is updated, the observer
will be called
being passed the new attribute value (as newValue
) and the old attribute value during the time when the observer
was previously called,
as (oldValue
).
Returns a RBXScriptConnection object.
instanceAttributeUtil.observe(workspace, "SomeAttribute", function(newValue, oldValue)
...
end)
instanceAttributePromise
instanceAttributeUtil.
instanceAttributePromise
(
) →
Promise
<
value:
any
>
Returns a promise which is resolved when the given instance has the given attribute.
instanceAttributeUtil.instanceAttributePromise(instance, "SomeAttribute"):andThen(function(value)
print(value) --> 5
end)
instance:SetAttribute("SomeAttribute", 5)
predicate
can also be passed, which should return a boolean value. It is passed
the new attribute value of the instance, and the old attribute value of the instance. The promise
will only resolve when predicate
returns true
for the new attribute value of the instance.
The returned promise will be cancelled if instance
is destroyed.
instanceAttributeUtil.instanceAttributePromise(instance, "SomeAttribute", function(newValue, oldValue)
return newValue == 2 and oldValue == 1
end):andThen(function(value)
print(value) --> 2
end)
instance:SetAttribute("SomeAttribute", 1)
instance:SetAttribute("SomeAttribute", 2)
instanceAttributesPromise
instanceAttributeUtil.
instanceAttributesPromise
(
attributes:
{
string
}
) →
Promise
<
>
Calls instanceAttributeUtil.instanceAttributePromise for every attribute in attributes
.
Returns a promise that is resolved once attributes in instance
are non-nil.
instanceAttributeUtil.instanceAttributesPromise(workspace.Part, {"Effect", "Target"}):andThen(function()
print(workspace.Part:GetAttribute("Effect")) --> "Fire"
print(workspace.Part:GetAttribute("Target")) --> "Player1"
end)
workspace.Part:SetAttribute("Effect", "Fire")
workspace.Part:SetAttribute("Target", "Player1")
setInstanceAttributes
Sets the attributes of instance
from the attributes
table.
instanceAttributeUtil.setInstanceAttributes(workspace.Baseplate, {IsMayoSauce = true, Test = 123})
print(workspace.Baseplate:GetAttributes()) --> {IsMayoSauce = true, Test = 123}