Skip to main content

Property

A class for wrapping values around setters and getters. A property in layman's terms is simply an object which contains some value.

local property = Property.new(5)
print(property:get()) --> 5

property.onUpdate:Connect(function(newValue, oldValue)
	print(newValue, oldValue) --> 10, 5
end)

property:set(10) 

Types

Property

type Property = Property

Properties

onUpdate<T>

This item is read only and cannot be modified. Read OnlySignalProperty Instance
Property.onUpdate<T>: Signal<newValueT,oldValueT?>

A signal which is fired whenever the value of the property is set to a new one. The signal is only passed the new value as the only argument.

Functions

new

Property.new(initialValueT?) → Property

A constructor method that creates a new property object, with initialValue as the current value of the property.

is

Property.is(selfany) → boolean

A method that returns a boolean indicating if self is a property or not.

clone

Property Instance
Property:clone(mapper((
newValueT,
oldValueT?
) → any)?) → ()

Creates a new property and binds it to the current property, and returns the new property. The new property will operate on the value of the current property, however you may specify a mapper function for more control. The mapper function will be passed the new value and the old value of the current property (and when the current property is updated), the return value of the mapper will be the new value of the new property.

The new property will be automatically destroyed once the current prpoerty is destroyed.

local property = Property.new(5)
local anotherProperty = property:clone(function(number)
	return number + 5
end)

print(anotherProperty:get()) --> 10
property:set(10)
print(anotherProperty:get()) --> 15

bindToInstanceProperty

Property Instance
Property:bindToInstanceProperty(
instanceInstance,
propertystring
) → ()

Binds the property to a property of the given Roblox instance. When the instance is destroyed, the property is automatically destroyed as well.

local property = Property.new()
property:bindToInstanceProperty(workspace.Baseplate, "Transparency")

property.onUpdate:Connect(function(newTransparency, oldTransparency)
	print(newTransparency, oldTransparency) --> 1, 0
end)

print(property:get()) --> 0

workspace.Baseplate.Transparency = 1

-- Deferred signal behavior: (Roblox instance-signals are not immediately fired)
print(property.onUpdate:Wait()) --> 1

bindToInstanceAttribute

Property Instance
Property:bindToInstanceAttribute(
instanceInstance,
attributestring
) → ()

Binds the property to an attribute of the given Roblox instance. When the instance is destroyed, the property is automatically destroyed as well.

local property = Property.new()
property:bindToInstanceAttribute(workspace.Baseplate, "SomeAttribute")

print(property:get()) --> nil

workspace.Baseplate:SetAttribute("SomeAttribute", 5)

print(property:get()) --> 5

bindToInstance

Property Instance
Property:bindToInstance(instanceInstance) → ()

Binds the property to an instance so that once the instance is destroyed, the property will be destroyed too.

local property = Property.new()
property:bindToInstance(workspace.Baseplate)

 print(Property.is(property)) --> true

workspace.Baseplate:Destroy()

task.wait() -- Deferred signal behavior

print(Property.is(property)) --> false

set

Property Instance
Property:set(valueT) → ()

Sets the value of the property to value, if this new value isn't the same as the previous value.

observe

Property Instance
Property:observe(callback(
newValueT,
oldValueT?
) → ()) → ()

Observes the value of the property.

property:observe(function(newValue, oldValue)

end)

valuePromise

Property Instance
Property:valuePromise(valuePredicate((
newValueT,
oldValueT?
) → boolean)?) → Promise

Returns a promise which is resolved with a non-nil value of the property, given that valuePredicate is not passed as an argument.

local property = Property.new()

property:valuePromise():andThen(function(value)
	print(value) --> 2
end)

property:set(2)

valuePredicate can also be passed, which allows you to filter out values. If it returns exactly true, only then will the promise resolve with the new value.

property:valuePromise(function(newValue, oldValue) 
	return newValue == 10 and oldValue == 1
end):andThen(function(newValue)
	print(newValue) 
end)

property:set(1) 
property:set(10) 
--> 10
NOTE

The returned promise will be cancelled as soon as the property object is destroyed.

forceSet

Property Instance
Property:forceSet(valueT) → ()

Works the same as Property:set except that values aren't checked for equality, for e.g:

local property = Property.new()

property.onUpdate:Connect(function(newValue)
	print(newValue) 
end)
property:forceSet(1) 
property:forceSet(2) 
property:forceSet("a") 

--> 1
--> 2
--> "a"

silentSet

Property Instance
Property:silentSet(valueT) → ()

Works almost the same as Property:set, but never fires off the Property.onUpdate signal.

get

Property:get() → T

Returns a shallow copy of the current value of the property.

local property = Property.new()

local t = {}
property:set(t)
print(property:get() == t) --> false

destroy

Property Instance
Property:destroy() → ()

Destroys the property and renders it unusable.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "A constructor method that creates a new property object, with `initialValue` as the current value\nof the property.",
            "params": [
                {
                    "name": "initialValue",
                    "desc": "",
                    "lua_type": "T?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Property"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 51,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "is",
            "desc": "A method that returns a boolean indicating if `self` is a property or not.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 70,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "clone",
            "desc": "Creates a new property and binds it to the current property, and returns the new\nproperty. The new property will operate on the value of the current property, however\nyou may specify a `mapper` function for more control. The `mapper` function will be \npassed the new value and the old value of the current property (and when the current\nproperty is updated), the return value of the mapper will be the new value of the new\nproperty.\n\nThe new property will be automatically destroyed once the current prpoerty is destroyed.\n\n```lua\nlocal property = Property.new(5)\nlocal anotherProperty = property:clone(function(number)\n\treturn number + 5\nend)\n\nprint(anotherProperty:get()) --> 10\nproperty:set(10)\nprint(anotherProperty:get()) --> 15\n```",
            "params": [
                {
                    "name": "mapper",
                    "desc": "",
                    "lua_type": "((newValue: T, oldValue: T?) -> any)?"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 98,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "bindToInstanceProperty",
            "desc": "Binds the property to a property of the given Roblox instance. When the instance is destroyed,\nthe property is automatically destroyed as well.\n\n```lua\nlocal property = Property.new()\nproperty:bindToInstanceProperty(workspace.Baseplate, \"Transparency\")\n\nproperty.onUpdate:Connect(function(newTransparency, oldTransparency)\n\tprint(newTransparency, oldTransparency) --> 1, 0\nend)\n\nprint(property:get()) --> 0\n\nworkspace.Baseplate.Transparency = 1\n\n-- Deferred signal behavior: (Roblox instance-signals are not immediately fired)\nprint(property.onUpdate:Wait()) --> 1\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "property",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 133,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "bindToInstanceAttribute",
            "desc": "Binds the property to an attribute of the given Roblox instance. When the instance is destroyed,\nthe property is automatically destroyed as well.\n\n```lua\nlocal property = Property.new()\nproperty:bindToInstanceAttribute(workspace.Baseplate, \"SomeAttribute\")\n\nprint(property:get()) --> nil\n\nworkspace.Baseplate:SetAttribute(\"SomeAttribute\", 5)\n\nprint(property:get()) --> 5\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "attribute",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 159,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "bindToInstance",
            "desc": "Binds the property to an instance so that once the instance is destroyed,\nthe property will be destroyed too.\n\n```lua\nlocal property = Property.new()\nproperty:bindToInstance(workspace.Baseplate)\n\n print(Property.is(property)) --> true\n\nworkspace.Baseplate:Destroy()\n\ntask.wait() -- Deferred signal behavior\n\nprint(Property.is(property)) --> false\n```",
            "params": [
                {
                    "name": "instance",
                    "desc": "",
                    "lua_type": "Instance"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 187,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "set",
            "desc": "Sets the value of the property to `value`, if this new value isn't the same as the previous value. ",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 199,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "observe",
            "desc": "Observes the value of the property. \n\n```lua\nproperty:observe(function(newValue, oldValue)\n\nend)\n```",
            "params": [
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(newValue: T, oldValue: T?) -> ()"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 223,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "valuePromise",
            "desc": "Returns a promise which is resolved with a non-nil value of the property, given that\n`valuePredicate` is not passed as an argument.\n\n```lua\nlocal property = Property.new()\n\nproperty:valuePromise():andThen(function(value)\n\tprint(value) --> 2\nend)\n\nproperty:set(2)\n```\n\n`valuePredicate` can also be passed, which allows you to filter out values. If it returns\nexactly `true`, only then will the promise resolve with the new value.\n\n```lua\nproperty:valuePromise(function(newValue, oldValue) \n\treturn newValue == 10 and oldValue == 1\nend):andThen(function(newValue)\n\tprint(newValue) \nend)\n\nproperty:set(1) \nproperty:set(10) \n--> 10\n```\n\n:::note\nThe returned promise will be cancelled as soon as the property object is destroyed.\n:::",
            "params": [
                {
                    "name": "valuePredicate",
                    "desc": "",
                    "lua_type": "((newValue: T, oldValue: T?) -> boolean)?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 276,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "forceSet",
            "desc": "Works the same as [Property:set] except that values aren't checked for equality, for e.g:\n\n```lua\nlocal property = Property.new()\n\nproperty.onUpdate:Connect(function(newValue)\n\tprint(newValue) \nend)\nproperty:forceSet(1) \nproperty:forceSet(2) \nproperty:forceSet(\"a\") \n\n--> 1\n--> 2\n--> \"a\"\n```",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 332,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "silentSet",
            "desc": "Works almost the same as [Property:set], but never fires off the [Property.onUpdate] signal.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 344,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "get",
            "desc": "Returns a shallow copy of the current value of the property.\n\n```lua\nlocal property = Property.new()\n\nlocal t = {}\nproperty:set(t)\nprint(property:get() == t) --> false\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 360,
                "path": "src/Property/init.luau"
            }
        },
        {
            "name": "destroy",
            "desc": "Destroys the property and renders it unusable.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "tags": [
                "Property Instance"
            ],
            "source": {
                "line": 374,
                "path": "src/Property/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "onUpdate<T>",
            "desc": " \n\nA [signal](https://sleitnick.github.io/RbxUtil/api/Signal/) which is fired whenever the value of the property is \nset to a new one. The signal is only passed the new value as the only argument.",
            "lua_type": "Signal <newValue: T, oldValue: T?>",
            "tags": [
                "Signal",
                "Property Instance"
            ],
            "readonly": true,
            "source": {
                "line": 28,
                "path": "src/Property/init.luau"
            }
        }
    ],
    "types": [
        {
            "name": "Property",
            "desc": " ",
            "lua_type": "Property",
            "source": {
                "line": 33,
                "path": "src/Property/init.luau"
            }
        }
    ],
    "name": "Property",
    "desc": " \n\nA class for wrapping values around setters and getters. A property in layman's terms is simply an object which contains some value.\n \n```lua\nlocal property = Property.new(5)\nprint(property:get()) --> 5\n\nproperty.onUpdate:Connect(function(newValue, oldValue)\n\tprint(newValue, oldValue) --> 10, 5\nend)\n\nproperty:set(10) \n```",
    "source": {
        "line": 17,
        "path": "src/Property/init.luau"
    }
}