Participants
Updated
Participants tells a script who is attending the experience: each person’s name, whether it is you, whether their hand is raised and whether they are speaking. Use it for a roster panel, a “3 people here” counter, a speaker highlight or a raise-hand button. It reads attendance only; it does not depend on which scenes people have loaded, on avatars or on media streams.
Before you start
Section titled “Before you start”- You know what a room is. See the Multiplayer overview.
- You can show text in a UI Label. See UI.
- The difference with
Network.peers:Networklists the peers sharing objects in the room;Participantsdescribes the people attending, with their names and status.
Reading the roster
Section titled “Reading the roster”| Member | What it does |
|---|---|
Participants. |
The current snapshot: an unchanging picture of the roster at this moment. |
Participants. |
Call listener(snapshot) right away and again each time the roster changes. Returns a function that stops it; it also stops when the script is released. |
Participants. |
Raise (true) or lower (false) your own hand. Resolves { status }. |
A snapshot never changes after you get it: to see changes, read a new one or subscribe.
The snapshot
Section titled “The snapshot”| Field | What it is |
|---|---|
participants |
The people attending, as a list of participants. |
count |
How many participants there are. |
completeness |
'complete' when the roster is known to be the full list, 'partial' when it may be missing people. |
raised |
How many hands are raised, when every hand is known; undefined otherwise. |
local |
Whether your own hand is shown raised. |
revision |
A number that grows with each new snapshot. |
Completeness, plainly. The roster is 'complete' while this peer is connected to the room and knows everyone in it. It becomes 'partial' when the connection is lost or not yet made: the list is then emptied or out of date. Show the count as exact only when the roster is complete.
A participant
Section titled “A participant”| Field | What it is |
|---|---|
id |
A unique id for this attendance. Each browser tab is its own participant, even for the same account. |
displayName |
The person’s name, when known. Names are for display, not identification: two people may share one. |
isLocal |
Whether this participant is you. |
handRaised |
Whether their hand is raised; undefined when unknown. |
speaking |
Whether they are speaking now; undefined when their voice connection is unknown. |
role |
'host' or 'guest', when the room gives roles. |
profile |
Extra profile data, such as profile., a picture URL, when there is one. |
world.state |
Their membership of the room: 'connected', 'absent' or 'unknown'. |
media.state |
Their voice and video connection: 'connected', 'absent' or 'unknown'. |
A person who leaves the room leaves the roster, even if their voice connection lingers.
Raising a hand
Section titled “Raising a hand”setLocalHandRaised(raised) changes your own hand only. It resolves an object whose status is:
| Status | Meaning |
|---|---|
'confirmed' |
The room confirmed the change, or, while not connected, the change was made locally. |
'unavailable' |
The room does not support raised hands. |
'busy' |
Another change is still in progress. |
'failed' |
The change did not go through. |
While connected, the room confirms the flag and every participant sees it through handRaised. While disconnected, the change is local and only affects localHandRaised; it is not sent later. It works in play only.
Examples
Section titled “Examples”1. A people counter
Section titled “1. A people counter”#pragma lifecycle(startup)
function startup() { const count = findEntity('Count').getComponent(UILabelComponent);
const first = Participants.getSnapshot(); log(`${first.count} people when the script started`);
Participants.subscribe((snapshot) => { const approx = snapshot.completeness === 'complete' ? '' : ' (updating)'; count.setProps({ text: `${snapshot.count} here${approx}` }); });}Reads the roster once with Participants.getSnapshot, then keeps a UI Label up to date with Participants.subscribe, flagging a roster that is still updating.
Read the label and the log. The preview has no room, so the roster is empty; in a published world the count follows people as they come and go.
getSnapshotreads the roster once, at this moment.subscribecalls the listener at once, so the label is filled immediately and then kept up to date.- When the roster is partial, the text says so rather than showing a wrong number as fact.
Snippets
Section titled “Snippets”1. A roster with hands and speakers
Section titled “1. A roster with hands and speakers”#pragma lifecycle(startup)
// Scene: a "Roster" UI Label with maxWidth set, inside a UI Panel.let roster;
function line(person) { const marks = []; if (person.isLocal) marks.push('you'); if (person.role === 'host') marks.push('host'); if (person.handRaised) marks.push('✋'); if (person.speaking) marks.push('speaking'); const name = person.displayName ?? 'Guest'; return marks.length ? `${name} (${marks.join(', ')})` : name;}
function startup() { roster = findEntity('Roster').getComponent(UILabelComponent);
Participants.subscribe((snapshot) => { const lines = snapshot.participants .filter((person) => person.world.state === 'connected') .map(line); const hands = snapshot.raisedHandCount !== undefined ? `, ${snapshot.raisedHandCount} hands up` : ''; roster.setProps({ text: `${snapshot.count} people${hands}\n${lines.join('\n')}` }); });}- Each line is built from the participant’s fields;
displayNamecan be missing, so a fallback is used. raisedHandCountis shown only when it is known.- Because
speakingchanges are part of the snapshot, the roster also highlights who is talking.
2. A raise-hand toggle
Section titled “2. A raise-hand toggle”#pragma lifecycle(startup)
// Scene: a "Hand" UI Toggle Button with a Signal Emitter on 'hand/toggle'.let toggle;let busy = false;let syncing = false;
// Show a state on the button without treating it as a press.function showRaised(raised) { if (toggle.isChecked() === raised) return; syncing = true; toggle.setChecked(raised); syncing = false;}
function startup() { toggle = findEntity('Hand').getComponent(UIToggleButtonComponent);
// Keep the button in step with the confirmed state, also when a host lowers our hand. Participants.subscribe((snapshot) => { if (!busy) showRaised(snapshot.localHandRaised === true); });
on('hand/toggle', async (event) => { if (event.eventType !== EventType.UIToggleButton.Change || busy || syncing) return; busy = true; const result = await Participants.setLocalHandRaised(event.data.checked); busy = false; if (result.status !== 'confirmed') { log('Hand change not applied:', result.status); showRaised(Participants.getSnapshot().localHandRaised === true); } });}- The toggle asks for the change; the subscription keeps it matching what the room confirmed.
setCheckedsends its ownchangeevent. Thesyncingflag marks those, so showing a state never sends a new request.busykeeps the subscription from flipping the button back while a request is in flight.- If the room refuses, the button returns to the confirmed state.
Common mistakes
Section titled “Common mistakes”- Using names as ids. Two people can share a name, and one person can attend twice. Use
idto tell participants apart. - Trusting
countwhile partial. Checkcompletenessfirst. - Keeping an old snapshot. A snapshot is a picture of one moment; subscribe to stay current.
- Treating
undefinedasfalse.handRaisedandspeakingareundefinedwhen unknown; test with=== truewhen that matters.