Packages
State

State

The State class represents an object that wraps around a Roblox datatype. State is NOT immutable, meaning any/all data that the State is handling will NOT account for changes to that value outside of State.


There is quite a few features that have been bundled into State, however Developers do not need to take advantage of them all, here's a small rundown of what you can do with state:

  • Record/Save previous states
    • For an example, this can come in handy if you need to record the player keystrokes
  • QoL functions for mutating state
    • State implements several QoL functions (for ex: Increment, Decrement) to allow developers to quickly mutate state without getting and setting values.
  • Support for Roblox Attributes
    • State will track and update Roblox Attributes on an Object, this can help quite a bit to remove the Roblox boilerplate for tracking when an Attribute has changed.

Properties

Value

State.Value <any>

Changed

State.Changed <RBXScriptSignal (opens in a new tab)>

Destroyed

State.Destroyed <RBXScriptSignal (opens in a new tab)>

Methods

SetRecordingState

State:SetRecordingState( isRecording boolean (opens in a new tab) ) -> nil (opens in a new tab)

Sets the state of recording, when recording all states will be saved into a history of states

⚠️

A single state object can only save up to 15 previous states!

	local NumberState = State.new(0)
	
	NumberState:SetRecordingState(true)

GetRecord

State:GetRecord( count number? (opens in a new tab) ) -> { [number]: any }

Retrieves an array of previous states that have been set

	local NumberState = State.new(0)
	
	NumberState:SetRecordingState(true)
 
	for index = 1, 5 do
		NumberState:Set(index)
	end
 
	print(NumberState:GetRecord(3)) --> {
	--		[1] = 0,
	--		[2] = 1,
	--		[3] = ...
	--	}

Destroy

State:Destroy() -> nil (opens in a new tab)

Safe way to remove references to the Value as well as removing any generated content

	local Value = State.new(0)
 
	...
 
	Value:Destroy()

Set

State:Set( value any ) -> nil (opens in a new tab)

Set the value of a state, when setting a state the 'Changed' signal will invoke.

	local Value = State.new(0)
 
	Value:Set(1)

Increment

State:Increment( value number (opens in a new tab) ) -> nil (opens in a new tab)

Increments the value by a given input

	local value = State.new(5)
		:Increment(5)
 
	print(value:Get()) -- 10

Decrement

State:Decrement( value number (opens in a new tab) ) -> nil (opens in a new tab)

Decrement the value by a given input

	local value = State.new(10)
		:Decrement(5)
 
	print(value:Get()) -- 5

Concat

State:Concat( value string (opens in a new tab) ) -> nil (opens in a new tab)

Concat the value by a given input

	local Value = State.new("Hello ")
		:Concat("World!")
 
	print(value:Get()) -- Hello World!

Update

State:Update( transformFn (value: any) -> any ) -> nil (opens in a new tab)

Will change the value of the state to the result of the transform function

	local Value = State.new("Hello ")
		:Update(function(value)
			return value .. "World!"
		end)
 
	print(value:Get()) -- Hello World!

Get

State:Get() -> any

Fetches the value that the State currently holds.

ℹ️

As an alternative, State offers a .Value property which you can directly refer to.

	local Value = State.new(0)
	local resolve = Value:Get()

Observe

State:Observe( callbackFn (oldValue: any, newValue: any) -> () ) -> RBXScriptConnection (opens in a new tab)

Quick QoL function to observe any changes made to the states value, this will invok the callback function with the current value as soon as an :Observe call has been made.

⚠️

Be cautious when refering to the RBXScriptConnection :Observe returns, as the first callback function will be invoked before this connection is returned!

	local Value = State.new(0)
 
	Value:Observe(function(oldValue, newValue)
		doSomething(oldValue, newValue)
	end)

ToString

State:ToString() -> string (opens in a new tab)

Returns a prettified string version of the state table.

	local Value = State.new(0)
 
	print(tostring(Value)) -- Value<0>

Functions

new

State.new( value any ) -> State

Constructor function used to generate a new 'State' object

	local object = State.new("Hello, World!")
 
	...

fromAttribute

State.fromAttribute( object Instance (opens in a new tab), attribute string (opens in a new tab) ) -> State

Wrapper for State.new however wraps around a Roblox attribute, the State object will always have the latest attribute value.

	local object = State.fromAttribute(workspace.object, "attributeName")
 
	...

is

State.is( object State? ) -> boolean? (opens in a new tab)

Validate if an object is a 'State' object

	local object = State.new("Hello, World!")
 
	if State.is(object) then
		...
	end