Skip to content
Untitled Sandbox - Developer Documentation
docs/types/InstanceState

InstanceState<T>

A value that is kept separately for each instance and tells you when it changes, which you create with state.create_per_instance.

Events

changedEvent<(Instance, T, T)>

You listen to these with :connect, which the Event page covers.

InstanceState:get

method

Gives a copy of one instance's value, which still works for 5 seconds after it is destroyed.

InstanceState:get(instance: Instance) -> T
example
print(health:get(door))

Use it forchecking how much health a crate has left before deciding whether it breaks.

instanceThe instance.

InstanceState:set

method

Changes one instance's value, and is an error once that instance is destroyed.

InstanceState:set(instance: Instance, value: T) -> ()
example
health:set(door, 100)

Use it formarking a checkpoint as reached, or setting whether a door is locked.

instanceThe instance.
valueThe new value.

InstanceState:update

method

Hands your function one instance's value, and saves whatever it gives back.

InstanceState:update(instance: Instance, update: (T) -> T) -> ()
example
health:update(door, function(value) return value - 10 end)

Use it forchanging a tower's upgrade level when its data is stored together as a table.

instanceThe instance.
updateGets the value and returns the new one.

InstanceState:increment

method

Adds to one instance's number value, or takes away, and gives back the new value.

InstanceState:increment(instance: Instance, amount: number?) -> number
example
health:increment(door, -10)

Use it fortaking a bit of health off a wall each time a player hits it.

instanceThe instance.
amountoptionalHow much to add, 1 by default. A negative amount takes away.
↑ ↓ moveEnter openEsc close