Respawn Timer Countdown

I have made a respawn timer, but I want it to print how much time they have left to respawn in the middle of their screen. It prints 1:38, but it doesn’t countdown. Here is my code:

[lua]function GM:PlayerDeath( ply, weapon, killer ) --Victim, weapon, killer
if ply:IsPlayer() && ply:IsValid() && killer:IsPlayer() && killer:IsValid() && weapon:IsValid() then
for k, v in pairs( player.GetAll() ) do
if v:IsAdmin() then
v:PrintMessage( HUD_PRINTCONSOLE, tostring( killer ) … " killed " … ply:Nick() )
end
end
end

ply.RespawnTime = CurTime() + 60

end

function GM:PlayerDeathThink( ply )
ply.RespawnTime = ply.RespawnTime or 0

ply:PrintMessage( HUD_PRINTCENTER, string.ToMinutesSeconds( ply.RespawnTime - 1 ) )
if ply.RespawnTime < CurTime() then
	ply:Spawn()
end

end[/lua]

You will mainly want to look at the PlayerDeathThink hook. Thanks.

If you want it to count down, you’ll want to add some lines to the clientside HUDPaint hook via hook.Add(). Send the client a usermessage informing it at what time relative to CurTime() (which is predicted and synchronized) the player can spawn.

[lua]
–clientside
–you can just modify the function if you don’t want to hook
local surface = surface; --optimize
local draw = draw;
local rescol = Color(255, 0, 0, 255); --red
hook.Add(“HUDPaint”, “hudrestime”, function()
if (RespawnTime) then
local dif = CurTime() - RespawnTime;
if (dif > 0) then
draw.SimpleText(string.ToMinutesSeconds(dif), “Default”, surface.ScreenWidth()/2, surface.ScreenHeight()/2, rescol, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER);
end
end
end);

–hook the usermessage to receive respawn time
usermessage.Hook(“getres”, function(msg)
local time = msg:ReadLong();
if (time == 0) then RespawnTime = nil; else RespawnTime = time; end
end);
[/lua]

[lua]
–serverside
function GM:PlayerDeath( ply, weapon, killer ) --Victim, weapon, killer
if ply:IsPlayer() && ply:IsValid() && killer:IsPlayer() && killer:IsValid() && weapon:IsValid() then
for k, v in pairs( player.GetAll() ) do
if v:IsAdmin() then
v:PrintMessage( HUD_PRINTCONSOLE, tostring( killer ) … " killed " … ply:Nick() )
end
end
end

ply.RespawnTime = CurTime() + 60
umsg.Start("getres", ply)
    umsg.Long(ply.RespawnTime)
umsg.End()

end

function GM:PlayerDeathThink(ply)
if (ply.RespawnTime && ply.RespawnTime < CurTime()) then
umsg.Start(“getres”, ply)
umsg.Long(0)
umsg.End()
ply:Spawn()
end
end
[/lua]

There are a few problems I spent last night and a hour this morning trying to fix. First one is the timer only displays if the player is alive. Also, when the timer is being displayed, it counts up, not down. I couldn’t fix it, can someone? I used Slight’s code.

Do something like this to stop timer showing if people are dead.

[Lua]if !LocalPlayer():IsAlive() then return end[/Lua]

In Slight’s code, you should do RespawnTime - CurTime() because CurTime is then currently smaller than respawntime, and RTM, there is no meta function called IsAlive and even if you were to use **[Player.Alive

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.Alive)** in your code it would stop the timer when players are dead, when he wants the timer to stop when they are alive.

Thanks, leiftiger, it worked. Thanks for the try, BEAST, but I didn’t need it. And most of all, thanks Slight.

Yea i just re read your post and noticed where I went wrong.

Sorry

I found this thread very interesting. Thanks for everyone who helped Wakeboarder. And thanks for making the thread up.

Solved ^^