Timer help.

So, I need help with this, i’ve just started coding a few days ago and i’m making a simple gamemode (Don’t ask haha) to start off with , and i’m a slow learner I guess you could say. So, what I need help with is I have this timer in init.lua

[lua]
local Time = 0

timer.Create(“Timer”, 0, 0, function( pl )
Time = Time + 1
CTime = CurTime(Time)
end )

hook.Add( “PlayerSpawn”, “Timer”, Timer)
[/lua]

I suspect I did this wrong in some way, but it works the way I want it too nonetheless.

Next, I made an entity, and added this to the init.lua

[lua]
function ENT:Touch(ply)
ply:PS_GivePoints(2000)
for k, v in pairs(player.GetAll()) do
v:ChatPrint( ply:Nick() … " finished with a time of “…(string.ToMinutesSeconds(CTime))…” w00t!")
timer.Stop(“Timer”)
ply:Kill( ply )
ply:Spawn( ply )
timer.Start(“Timer”)
end
end
[/lua]

And it works correctly when the player touches the entity, but, what I don’t know how to do is reset the timer back to 0 and start again when the player touches the entity and respawns. When I touch the entity again it just kept adding to the time it showed before. Can some one help me a n00b out please?

Thanks in advance!

I don’t even understand what you’re trying to use the timer for.

CurTime() doesn’t take an argument, so giving it an argument doesn’t do anything. CurTime() shows how long the server has been running, so if you’re going to use it, you’re using it wrong.

If your making a gamemode, you dont have to add hooks.

Try doing something like this.
[LUA]

function GM:PlayerSpawn(Player)
Player.StartTime = CurTime()
end

[/LUA]

[LUA]
function ENT:Touch(ply)
ply:PS_GivePoints(2000)
local Message = ply:Nick() … " finished with a time of “…(string.ToMinutesSeconds(CurTime()-ply.StartTime))…” w00t!")
for k, v in pairs(player.GetAll()) do
v:ChatPrint( Message)
ply:Kill()
ply:Spawn()
end
end

[/LUA]

When the player makes it to the end of the map and touches the entity, It will show the total time it took for the player to make it to the end of the map and display it in Chat. That’s all i’m using it for really.

You don’t need to kill and spawn the player in the loop.

[editline]15th January 2011[/editline]

Unless you want to kill and spawn everyone, where ply should be v.

I fixed this. The timer resets properly every time the player spawns, but can anyone tell me how I would make it not reset every time a player spawns/respawns? Like, I want each player to spawn with their own timer. I tried timer.Create(v:UniqueID()…“Timer” with no luck there, can anyone help further with this?

Also, I didn’t use it, but thanks anyways -TB-.

What I posted does this already. And much more effeciently.