Anti spawn kill sometimes glitching

Hello,

I manage a murder server and I’m trying to learn lua to spice things up a bit, but most of the time I just have no idea what I’m doing.
I wanted a simple ~10sec spawn protection, so I took this code and modded it a bit to display when the spawn protection is ON/OFF. However sometimes (I guess when someone joins the server) during a round, the messages “Spawn protection ON/OFF” appear and I don’t really understand why…

Here is the modded code I’m using (Please go ahead and say my code is terrible, including clues as to how I could fix it if possible) :



hook.Add( "PlayerSpawn", "SimpleSpawnProtection.PlayerSpawn", function( ply )
  ply:GodEnable() -- Enable god mode on the player.
  timer.Simple ( 8, function()
	timer.Create ( "spawnON" , 1, 11, function()
		PrintMessage( HUD_PRINTCENTER, "Spawn protection ON")
	end )
  end )
  timer.Simple( 20, function() -- Starting timer
    if ( IsValid( ply ) ) then -- We must ensure a player object remained valid after delay (it would be invalid and cause script errors if a player have disconnected from the server)..
      ply:GodDisable() -- Disable god mode on the player.
	  PrintMessage( HUD_PRINTCENTER, "Spawn protection OFF")
    end
  end )
end )


Thank you !

Check if a round is in progress and or check if the player is alive before doing anything?

PrintMessage sends to all players - you’ll want to use

Player:PrintMessage instead.

Hey, thanks for the input.

I changed the code to



hook.Add( "PlayerSpawn", "SimpleSpawnProtection.PlayerSpawn", function( ply )
 if ply:Alive() then
  ply:GodEnable() -- Enable god mode on the player.
  timer.Simple ( 8, function()
	timer.Create ( "spawnON" , 1, 11, function()
		PrintMessage( HUD_PRINTCENTER, "Spawn protection ON")
	end )
  end )
  timer.Simple( 20, function() -- Starting timer
    if ( IsValid( ply ) ) then -- We must ensure a player object remained valid after delay (it would be invalid and cause script errors if a player have disconnected from the server)..
      ply:GodDisable() -- Disable god mode on the player.
	  PrintMessage( HUD_PRINTCENTER, "Spawn protection OFF")
    end
  end )
 end
end )


I tried using Player:PrintMessage but it gave me errors :



[ERROR] lua/autorun/server/spawnprotect.lua:6: attempt to index global 'Player' (a function value)
  1. unknown - lua/autorun/server/spawnprotect.lua:6

Timer Failed! [spawnON][@lua/autorun/server/spawnprotect.lua (line 5)]

[ERROR] lua/autorun/server/spawnprotect.lua:6: attempt to index global 'Player' (a function value)
  1. unknown - lua/autorun/server/spawnprotect.lua:6

Timer Failed! [spawnON][@lua/autorun/server/spawnprotect.lua (line 5)]

[ERROR] lua/autorun/server/spawnprotect.lua:12: attempt to index global 'Player' (a function value)
  1. unknown - lua/autorun/server/spawnprotect.lua:12

Timer Failed! [Simple][@lua/autorun/server/spawnprotect.lua (line 9)]

[ERROR] lua/autorun/server/spawnprotect.lua:12: attempt to index global 'Player' (a function value)
  1. unknown - lua/autorun/server/spawnprotect.lua:12

Timer Failed! [Simple][@lua/autorun/server/spawnprotect.lua (line 9)]


I haven’t tested the new code yet since the server is empty, but will do tomorrow ! Thanks.

don’t use “Player:PrintMessage” literally Player should be the variable that holds player, in your case “ply”, so your code should be changed from “PrintMessage” to “ply:PrintMessage”

Ohh. I see.

Some people joined so I was able to test.

“if ply:Alive() then” did nothing, it was still glitchy. However using ply:PrintMessage instead of just PrintMessage seems to have fixed it.

Thank you !

That hook is called instantly when the player spawns, and there isn’t such a thing as a player “spawning dead” (at least in my experience), so the “if ply:Alive() then” part is unnecessary.