Prevent Respawn

How do you prevent a player from respawning?

**[Gamemode.PlayerDeathThink

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerDeathThink)**

If you want to prevent from spawning then just return non-nil value.

So would this work?



function playerforcerespawn( ply )
 
     if (CurTime()>ply.nextsecond) then              --If the current time has execeeded our next second add time
          if (ply.deathtime < maxdeathtime) then     --If the number of seconds dead less than our max death time
               ply.deathtime = ply.deathtime + 1;    --add one to the number of seconds dead
               ply.nextsecond = CurTime()+1;         --set our next death second add time
                return true
          else                                       --Otherwise
               ply.deathtime = nil;                  --erase our deathtime variable by seting to nil
               ply.nextsecond = nil;                 --erase our new add time storage by seting to nil
               ply:Spawn()                           --tell the player to spawn
          end
    end
 
end
 
hook.Add( "PlayerDeathThink", "player_step_forcespawn", playerforcerespawn );


I also tried this but I can still attack and jump.



function GM:PlayerBindPress( ply, bind, pressed )
    --if (ply.deathtime < maxdeathtime) then 
          //To block more commands, you could add another line similar to the one below, just replace the command
          if string.find( bind, "+attack" ) then return true end
          if string.find( bind, "+attack2" ) then return true end
          if string.find( bind, "+jump" ) then return true end
    --end
end


[editline]11:15PM[/editline]

I got it working with this:



function GM:PlayerDeathThink( ply )
     if (CurTime()>ply.nextsecond) then              --If the current time has execeeded our next second add time
          if (ply.deathtime < maxdeathtime) then     --If the number of seconds dead less than our max death time
               ply.deathtime = ply.deathtime + 1;    --add one to the number of seconds dead
               ply.nextsecond = CurTime()+1;         --set our next death second add time
          else                                       --Otherwise
               ply.deathtime = nil;                  --erase our deathtime variable by seting to nil
               ply.nextsecond = nil;                 --erase our new add time storage by seting to nil
               ply:Spawn()                           --tell the player to spawn
          end
    end
end


But how do I display the time remaining for respawn? Can I use surface.DrawText( “You’ll respawn in 1 minute and 30 seconds.” ) in the init.lua of the gamemode?

you could use a Networked Int in your client side of things that is the number of seconds left and use this http://wiki.garrysmod.com/?title=String.FormattedTime to format it into minutes and seconds.

I’m not sure if this is laggy or a terrible way to do this, so should I change it to networked int?
What I did was use user messages:

init.lua



--Death

maxdeathtime = 90;

function Death(ply)
    ply.deathtime = 0
    ply.nextsecond = CurTime() + 1
    ply.deathTimeLeft = maxdeathtime
    umsg.Start("gms_updaterespawntimer", ply)
    umsg.Short( ply.deathTimeLeft )
    umsg.End()
    umsg.Start("gms_startrespawntimer", ply)
    umsg.End()
    ply.NextSpawnTime = CurTime() + 400
    ply:ConCommand( "gms_dropall" )
    if ply.AFK == true then
        ply:Freeze(false)
        ply.AFK = false            
        umsg.Start("gms_stopafk",ply)
        umsg.End()
    end
end
hook.Add( "PlayerDeath", "Death", Death )

function GM:PlayerDeathThink( ply )
     if (CurTime()>ply.nextsecond) then              --If the current time has execeeded our next second add time
          if (ply.deathtime < maxdeathtime) then     --If the number of seconds dead less than our max death time
               ply.deathtime = ply.deathtime + 1;    --add one to the number of seconds dead
               ply.nextsecond = CurTime()+1;         --set our next death second add time
               ply.deathTimeLeft = maxdeathtime - ply.deathtime
               umsg.Start("gms_updaterespawntimer", ply)
               umsg.Short( ply.deathTimeLeft )
                umsg.End()
          else                                       --Otherwise
               ply.deathtime = nil;                  --erase our deathtime variable by seting to nil
               ply.nextsecond = nil;                 --erase our new add time storage by seting to nil
                   umsg.Start("gms_stoprespawntimer", ply)
                umsg.End()
               ply:Spawn()                           --tell the player to spawn
          end
    end
end



cl_init.lua



/*---------------------------------------------------------
   Respawn Timer
---------------------------------------------------------*/

function GM.RespawnTimerOverlay(data)
    if !RespawnFadeIn and !RespawnFadeOut then return end

    if RespawnFadeIn and RespawnFade < 250 then
        RespawnFade = RespawnFade + 5
    elseif RespawnFadeIn and RespawnFade >= 255 then
        RespawnFadeIn = false
    end

    if RespawnFadeOut and RespawnFade > 0 then
        RespawnFade = RespawnFade - 5
    elseif RespawnFadeOut and RespawnFade <= 0 then
        RespawnFadeOut = false
    end
    
    
    surface.SetDrawColor(0,0,0,RespawnFade)
    surface.DrawRect(0,0,ScrW(),ScrH())

    if (DeathTimeLeft != 0) then
        draw.SimpleText("Seconds left to respawn:   " .. DeathTimeLeft,"ScoreboardSub",ScrW() / 2, (ScrH() / 2) + (ScrH() / 4) , Color(255,255,255,RespawnFade),1,1)
    end
end
hook.Add( "HUDPaint", "RespawnTimerOverlay", GM.RespawnTimerOverlay )


function GM.StartRespawnTimer(um)
    RespawnFadeIn = true
    RespawnFade = 0
end

usermessage.Hook("gms_startrespawntimer",GM.StartRespawnTimer)

function GM.StopRespawnTimer(um)
    RespawnFadeOut = true
    RespawnFade = 255
end

usermessage.Hook("gms_stoprespawntimer",GM.StopRespawnTimer)

function GM.UpdateRespawnTimer(um)
    DeathTimeLeft = um:ReadShort()
end

usermessage.Hook("gms_updaterespawntimer",GM.UpdateRespawnTimer)


If you want to display timleft just use **[Entity.SetNetworkedInt

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt)**

You could also just send 1 usermessage to the player, telling him he died. Also send the time the player will be dead for. From that moment you start showing the information on the screen.

I want to make it have a nice user message by hooking the Hud Paint. But is my way too laggy or something?

[editline]08:02PM[/editline]

Well I took out the gms_updaterespawntimer and made it so it’s a networked integer that gets updated instead. I was guessing it would bring less strain on the server.