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
Properties
onUpdate<T>
This item is read only and cannot be modified. Read OnlySignalProperty InstanceProperty.onUpdate<T>:
Signal
<
newValue:
T,
oldValue:
T?
>
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
A constructor method that creates a new property object, with initialValue
as the current value
of the property.
is
Property.
is
(
self:
any
) →
boolean
A method that returns a boolean indicating if self
is a property or not.
clone
Property InstanceProperty:
clone
(
mapper:
(
(
newValue:
T
,
oldValue:
T?
)
→
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 InstanceBinds 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 InstanceBinds 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 InstanceBinds 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 InstanceProperty:
set
(
value:
T
) →
(
)
Sets the value of the property to value
, if this new value isn't the same as the previous value.
observe
Property InstanceProperty:
observe
(
callback:
(
newValue:
T
,
oldValue:
T?
)
→
(
)
) →
(
)
Observes the value of the property.
property:observe(function(newValue, oldValue)
end)
valuePromise
Property InstanceProperty:
valuePromise
(
valuePredicate:
(
(
newValue:
T
,
oldValue:
T?
)
→
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 InstanceProperty:
forceSet
(
value:
T
) →
(
)
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 InstanceProperty:
silentSet
(
value:
T
) →
(
)
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 InstanceProperty:
destroy
(
) →
(
)
Destroys the property and renders it unusable.