JavaScript multiplayer functions
Use these helpers when a game rule must address more than the current visitor. Build and test the single-player version first, then decide exactly what should be shared.
Know your data types
| Data type | Use it for | Example |
|---|---|---|
| Per-player parameter | A separate role, team, or slot for every player | Role = Explorer |
| Multiplayer numeric variable | One shared counter or score | TeamScore = 14 |
| Multiplayer task | One shared stage of a round | RoundStarted = Active |
Do not use the same name for different scopes.
Get the current player list
{{Players}} resolves to the players currently in the room. Pass it to the player-list helpers:
const players = {{Players}};
SetPlayersParameters(players, 'Role', 'Explorer');
Supported player-list helpers
| Function | Purpose |
|---|---|
| AssignNumbersInOrder(players, name) | Give players sequential numbers beginning at 1. |
| SelectRandomPlayers(players, count) | Choose a subset of a player list. |
| SelectPlayers(players, name, value) | Filter a list by a per-player parameter. |
| SelectPlayersParameters(players, name) | Return parameter values from a player list. |
| SetPlayersParameters(players, name, value) | Set a parameter on every player in a list. |
| SetPlayerParameter(name, value) | Set a parameter on the current player. |
| SelectPlayerParameter(name) | Read a parameter on the current player. |
| CountArray(list) | Count entries in a player list. |
| PrintString(list) | Turn a list into comma-separated text. |
Assign player slots
AssignNumbersInOrder({{Players}}, 'Slot');
Run this from one deliberate round-start path. Do not put it in a script that re-runs for every state change unless reassigning slots is intended.
Pick a role
const seeker = SelectRandomPlayers({{Players}}, 1);
SetPlayersParameters(seeker, 'Role', 'Seeker');
Test this with the smallest and largest room sizes your game supports. Selecting a role each time a player joins will produce unstable results unless that is your game design.
Filter and count
const blueTeam = SelectPlayers({{Players}}, 'Team', 'Blue');
const readyBluePlayers = SelectPlayers(blueTeam, 'Ready', true);
if (CountArray(readyBluePlayers) >= 2) {
SetTask('BlueTeamReady', 'Active', 0);
}
Make sure the value you filter on matches the value you set. For example, true is different from the text 'true'.
Turn player values into readable text
SelectPlayersParameters() returns a list. Use PrintString() when you need a
single text value from that list:
const blueTeam = SelectPlayers({{Players}}, 'Team', 'Blue');
const teamLabels = SelectPlayersParameters(blueTeam, 'Team');
SetVariable('BlueTeamLabels', PrintString(teamLabels), 0);
PrintString() returns comma-separated text. It expects a list; do not use it
as a developer log or pass it one plain text value. Every player in that list
must have the requested parameter, so filter the list by that same configured
parameter first.
Shared score recipe
Create TeamScore as a multiplayer numeric variable, then use the server-backed update helper:
UpdateMultiplayerNumericVariable('TeamScore', 1, 1, 0);
The operation values are:
| Operation | Meaning |
|---|---|
| 0 | Set |
| 1 | Add |
| 2 | Subtract |
| 3 | Multiply |
| 4 | Divide |
Use Add for each scored event. This avoids a local read-modify-write race when two players score at nearly the same time.
Shared score and timer leaderboard recipe
- Start the named timer through the no-code Start Timer effect when the round begins.
- Increment the multiplayer TeamScore using the Add operation.
- Stop the timer and post or display the result with the dedicated no-code leaderboard effects.
- Test two players scoring at the same time, a late join, and a round ending during an update.
Keep the timer and leaderboard setup in no-code effects. JavaScript Function should contain only the small rule that changes shared state.
Safe defaults
- Run player assignment from one explicit task rather than a repeating state-change script.
- Use stable names for parameters, tasks, and shared variables.
- Test with multiple real players before publishing.
- Keep player IDs inside the helpers; do not guess or hard-code another player’s ID.
