The position, rotation and scale of items are automatically synchronized by SubItem. In this page, we will explain how to synchronize other elements, such as on/off of a switch or a counter.
As an example, we will switch the on/off state of a switch with onUse (pressing on the grip button while grabbing a SubItem). When the switch is off, change the color of the material 0 to white, when on, change the color of the material 0 to red.
Use _ALL_ functions to run commands on all users simultaneously.
isRed = false -- not local function onUse() isRed = not isRed -- flip the true, false if isRed then vci.assets._ALL_SetMaterialColorFromIndex(0, Color.__new(1, 0, 0, 1)) -- Red else vci.assets._ALL_SetMaterialColorFromIndex(0, Color.__new(1, 1, 1, 1)) -- white end end
It seems simple, but it doesn't work in the following situations
Use the vci.state.
if vci.assets.isMine then -- Initialize with the item spawn, only in the user who spawned it vci.state.Set('isRed', false) -- Initial value. `nil` is set if nothing is done to it end function onUse() local isRed = vci.state.Get('isRed') -- Acquire true or false local vci.state.Set('isRed', not isRed) -- Just change the state, not the actual color end function updateAll() -- Use updateAll because this needs to be run on every users. local isRed = vci.state.Get('isRed') -- Acquire the state. The item sync variable will be the same in all users (synced) local if isRed then vci.assets.SetMaterialColorFromIndex(0, Color.__new(1, 0, 0, 1)) -- Red else vci.assets.SetMaterialColorFromIndex(0, Color.__new(1, 1, 1, 1)) -- white end end