Skip to main content

instanceAttributeUtil

A utility module for working with instance attributes.

Functions

all

instanceAttributeUtil.all(
instanceTreeInstance,
targetAttributestring
) → {Instance}

Returns a read only array of all descendants in instanceTree which have the targetAttribute attribute.

observeMany

instanceAttributeUtil.observeMany(
instanceInstance,
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

instanceAttributeUtil.observeDescendantAdding(
instanceTreeInstance,
targetAttributestring,
descendantAddedCallback(descendantInstance) → ()
) → RBXScriptConnection

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

instanceAttributeUtil.observeDescendantRemoving(
instanceTreeInstance,
targetAttributestring,
callback(descendantInstance) → ()
) → RBXScriptConnection

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(
instanceInstance,
targetAttributestring,
observer(
newAttributeValueany,
oldAttributeValueany
) → ()
) → 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(
instanceInstance,
attributestring,
predicate((
newValueany,
oldValueany
) → boolean)?
) → Promise<valueany>

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(
instanceInstance,
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

instanceAttributeUtil.setInstanceAttributes(
instanceInstance,
attributes{[string]any}
) → ()

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}
Show raw api
{
    "functions": [
        {
            "name": "all",
            "desc": "Returns a read only array of all descendants in `instanceTree` which have the\n`targetAttribute` attribute.",
            "params": [
                {
                    "name": "instanceTree",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "targetAttribute",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Instance }\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 15,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "observeMany",
            "desc": "Works similar to [instanceAttributeUtil.observe], but observes an array of  attributes instead of\na single attribute. \n\n```lua\ninstanceAttributeUtil.observeMany(workspace.Part, {\"Number1\", \"Number2\"}, function(newAttributes, oldAttributes)\n\tprint(newAttributes.Number1 + newAttributes.Number2)\nend)\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "attributesToObserve",
                    "desc": "",
                    "lua_type": "{ string }"
                },
                {
                    "name": "observer",
                    "desc": "",
                    "lua_type": "(\n\t\tnewAttributes: { [string]: any },\n\t\toldAttributeValue: { [string]: any }?\n\t) -> ()\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RBXScriptConnection\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 38,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "observeDescendantAdding",
            "desc": "Sets up an event connection which listens to any descendant added to `instanceTree`, if it has the\n`targetAttribute` atribute, then `targetAttribute` will be called. \n\nThis method will also capture the initial descendants of the given `instanceTree` as well.\n\n```lua\ninstanceAttributeUtil.observeDescendantAdding(workspace, \"Test\", function(instance)\n\tprint(instance)\nend)\n```",
            "params": [
                {
                    "name": "instanceTree",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "targetAttribute",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "descendantAddedCallback",
                    "desc": "",
                    "lua_type": "(descendant: Instance) -> ()\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RBXScriptConnection\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 97,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "observeDescendantRemoving",
            "desc": "Sets up an event connection which listens to any descendant removed from `instanceTree`, if it has the\n`targetAttribute` atribute, then `callback` will be called. Returns a [RBXScriptConnection](https://create.roblox.com/docs/reference/engine/datatypes/RBXScriptConnection) object.\n\n```lua\ninstanceAttributeUtil.observeDescendantRemoving(workspace, \"Test\", function(instance)\n\tprint(instance)\nend)\n```",
            "params": [
                {
                    "name": "instanceTree",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "targetAttribute",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(descendant: Instance) -> ()\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RBXScriptConnection\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 126,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "observe",
            "desc": "Observes the value of the attribute `targetAttribute` in `instance`. `observer` will initially be immediately called if `targetAttribute`\nexists in `instance`, being passed the attribute value as the only argument (`newAttributeValue`), whereas `oldAttributeValue` will be `nil`\nduring this time. Additionally from this point onwards, everytime `targetAttribute` in `instance` is updated, the `observer` will be called\nbeing passed the new attribute value (as `newValue`) and the old attribute value during the time when the `observer` was *previously* called,\nas (`oldValue`).\n\nReturns a [RBXScriptConnection](https://create.roblox.com/docs/reference/engine/datatypes/RBXScriptConnection) object.\n\n```lua\ninstanceAttributeUtil.observe(workspace, \"SomeAttribute\", function(newValue, oldValue)\n\t...\nend)\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "targetAttribute",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "observer",
                    "desc": "",
                    "lua_type": "(newAttributeValue: any, oldAttributeValue: any) -> ()\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RBXScriptConnection"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 156,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "instanceAttributePromise",
            "desc": "Returns a promise which is resolved when the given instance has the given attribute.\n\n```lua\ninstanceAttributeUtil.instanceAttributePromise(instance, \"SomeAttribute\"):andThen(function(value)\n\tprint(value) --> 5\nend)\n\ninstance:SetAttribute(\"SomeAttribute\", 5)\n```\n\n`predicate` can also be passed, which should return a boolean value. It is passed\nthe new attribute value of the instance, and the old attribute value of the instance. The promise\nwill only resolve when `predicate` returns `true` for the new attribute value of the instance.\n\nThe returned promise will be cancelled if `instance` is destroyed.\n\n```lua\ninstanceAttributeUtil.instanceAttributePromise(instance, \"SomeAttribute\", function(newValue, oldValue)\n\treturn newValue == 2 and oldValue == 1\nend):andThen(function(value)\n\tprint(value) --> 2\nend)\n\ninstance:SetAttribute(\"SomeAttribute\", 1)\ninstance:SetAttribute(\"SomeAttribute\", 2)\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "attribute",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "predicate",
                    "desc": "",
                    "lua_type": "((\n\t\tnewValue: any,\n\t\toldValue: any\n\t) -> boolean)?\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<value: any>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 206,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "instanceAttributesPromise",
            "desc": "Calls [instanceAttributeUtil.instanceAttributePromise] for every attribute in `attributes`. \nReturns a promise that is resolved once attributes in `instance` are non-nil.\n\n```lua\ninstanceAttributeUtil.instanceAttributesPromise(workspace.Part, {\"Effect\", \"Target\"}):andThen(function()\n\tprint(workspace.Part:GetAttribute(\"Effect\")) --> \"Fire\"\n\tprint(workspace.Part:GetAttribute(\"Target\")) --> \"Player1\"\nend)\n\nworkspace.Part:SetAttribute(\"Effect\", \"Fire\")\nworkspace.Part:SetAttribute(\"Target\", \"Player1\")\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "attributes",
                    "desc": "",
                    "lua_type": "{ string }"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 261,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        },
        {
            "name": "setInstanceAttributes",
            "desc": "Sets the attributes of `instance` from the `attributes` table.\n\n```lua\ninstanceAttributeUtil.setInstanceAttributes(workspace.Baseplate, {IsMayoSauce = true, Test = 123})\nprint(workspace.Baseplate:GetAttributes()) --> {IsMayoSauce = true, Test = 123}\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "attributes",
                    "desc": "",
                    "lua_type": "{ [string]: any }\n"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 283,
                "path": "src/instanceAttributeUtil/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "instanceAttributeUtil",
    "desc": "A utility module for working with instance attributes.",
    "source": {
        "line": 6,
        "path": "src/instanceAttributeUtil/init.luau"
    }
}