Player Death Command

Iv’e still got a few problems with my gamemode, but this one in particular is important, i do not know how to make people go to spectator team when they die, and i don’t know the lua function to make players spawn when they are all dead, any ideas on how?


function GM:PlayerDeath( ply, inflictor, attacker )

-- Set the player to spectate a random player in chase mode
ply:Spectate( OBS_MODE_CHASE )
ply:SpectateEntity( nil )

-- Assume everyone is dead
local allDead = true
-- Loop through every player and check if anyone's alive
for k, v in pairs( player.GetAll() ) do
	if v:Alive() then allDead = false break end

-- If everyone is dead then respawn everyone
if allDead then
	for k, v in pairs( player.GetAll() ) do
		v:Spectate( OBS_MODE_NONE )


If players are moved to spectator upon death, doesn’t that mean that when everyone is dead, the original team will have no players? If that’s the case, you could look at using team.GetPlayers and doing something along the lines of:

if team.GetPlayers( TEAM_EXAMPLE ) == 0 and team.GetPlayers( TEAM_SPECTATOR ) >= 1 then...

You’ve also got ply:Spawn() for spawning

I get this error when i use your code:

[ERROR] gamemodes/smash/gamemode/shared.lua:35: function arguments expected near ‘then’

  1. unknown - gamemodes/smash/gamemode/shared.lua:0


Hmm, when i die i go to spectator, but when i press a key to respawn, i respawn but stay in spectator mode for some reason, so basically i’m playing and spectating at the same time.
So everyone can still respawn.

Move them back to the team when respawning.