Module spawn
Spawn
This system manages player spawning and respawning in a multiplayer game session.
It supports timed respawns, custom spawn locations, team-based spawn grouping, and configurable player loadouts.
Core responsibilities:
- Automatically respawn dead players after a delay
- Allow forced respawns (e.g. at round start or by host)
- Select spawn positions from grouped spawn transforms
- Apply a configurable loadout upon respawn
Execution context:
- This system is intended to run server-side only
- Shared state is synced using the
shared.respawnTimeLefttable
Functions
| spawnInit () | Initialize the spawn system (server). |
| spawnSetSpawnTransforms (spawnTransforms[, groupIndex]) | Set spawn transforms for one or more groups (server). |
| spawnSetRespawnTime (respawnTime) | Set the automatic respawn delay in seconds (server). |
| spawnSetDefaultLoadout (loadout) | Set the default loadout to apply on respawn (server). |
| spawnSetRespawnAtCurrentLocation (active) | Enable or disable respawning at the player's last position (server). |
| spawnGetPlayerRespawnTimeLeft (player) | Get time left until a player will respawn. |
| spawnSpawnPlayer (transform, loadout, player) | Respawn a player at a given transform and with a given loadout (server). |
| spawnRespawnPlayer (playerId) | Flag a player for forced respawn on the next tick (server). |
| spawnRespawnAllPlayers () | Flag all players for forced respawn on the next tick (server). |
| spawnTick (dt[, playerGroupList]) | Main update loop for the spawn system (server). |
| spawnGetSpawnTransformGroups () | Get all current spawn transform groups (server). |
Functions
- spawnInit ()
-
Initialize the spawn system (server).
Resets the internal state. Call this once when setting up the game mode.
- spawnSetSpawnTransforms (spawnTransforms[, groupIndex])
-
Set spawn transforms for one or more groups (server).
Used to define where players can spawn based on group index (e.g. teams). If no
groupIndexis provided, all spawn points are assigned to group 1 and any previous group information is cleared.The transforms are of the same type { pos, rot } that is used in the API.
Parameters:
- spawnTransforms table List of spawn transforms.
- groupIndex number Group ID (defaults to 1). (optional)
Usage:
-- Set spawn transforms for 2 teams. spawnSetSpawnTransforms(spawnTransformTeam1, 1) spawnSetSpawnTransforms(spawnTransformTeam2, 2)
- spawnSetRespawnTime (respawnTime)
-
Set the automatic respawn delay in seconds (server).
Controls how long a player must remain dead before being automatically respawned by spawnTick.
Parameters:
- respawnTime number Time in seconds before respawn.
- spawnSetDefaultLoadout (loadout)
-
Set the default loadout to apply on respawn (server).
The loadout is a list of
{ toolName, ammoCount }entries, applied in order. All tools are disabled and cleared first, then tools in the loadout are enabled and given the specified ammo. The first entry becomes the active tool.Parameters:
- loadout
table
List of
{ toolName, ammoCount }tables.
Usage:
function server.init() defaultLoadout = { {"gun", 7}, {"sledge", 0}, {"spraycan", 0}, {"extinguisher", 0} } spawnSetDefaultLoadout(defaultLoadout) end
- loadout
table
List of
- spawnSetRespawnAtCurrentLocation (active)
-
Enable or disable respawning at the player's last position (server).
When enabled, players are respawned at the transform where they died instead of at a spawn point chosen from
spawnTransformGroups.Parameters:
- active
bool
trueto enable,falseto disable.
- active
bool
- spawnGetPlayerRespawnTimeLeft (player)
-
Get time left until a player will respawn.
When the player is alive, this returns
0. While dead, the value is updated every tick and rounded up to whole seconds.Parameters:
- player number Player ID.
Returns:
-
number
Time in seconds until respawn, or
0when alive. - spawnSpawnPlayer (transform, loadout, player)
-
Respawn a player at a given transform and with a given loadout (server).
If
transformisnil, the player is respawned using the engine's default spawn position. When aloadoutis provided those tools will be equipped and the current tools will be removed.Parameters:
- spawnRespawnPlayer (playerId)
-
Flag a player for forced respawn on the next tick (server).
The player will be respawned immediately during the next spawnTick call, regardless of how long they have been dead.
Parameters:
- playerId number Player ID.
- spawnRespawnAllPlayers ()
-
Flag all players for forced respawn on the next tick (server).
Useful for round start or when resetting global game state.
- spawnTick (dt[, playerGroupList])
-
Main update loop for the spawn system (server).
Should be called every tick. For each player it:
- Tracks time spent dead
- Automatically respawns players once they exceed the configured respawn time
- Performs forced respawns flagged via spawnRespawnPlayer / spawnRespawnAllPlayers
If respawning at current location is disabled, a spawn transform is chosen from the group-based spawn lists using
spawnPickSpawnTransform.Parameters:
- dt number Time step in seconds.
- playerGroupList table Table mapping player IDs to group indices. (optional)
Usage:
local playerGroupList = {} playerGroupList[1] = 1 -- player id 1,2,3 is in team 1 and 4,5,6 in team 2 playerGroupList[2] = 1 playerGroupList[3] = 1 playerGroupList[4] = 2 playerGroupList[5] = 2 playerGroupList[6] = 2 spawnTick(dt, playerGroupList)
- spawnGetSpawnTransformGroups ()
-
Get all current spawn transform groups (server).
Returns:
-
table
Table mapping group indices to lists of spawn transforms.