Last Lua " Timer "

Hello guys, well a few days ago i make a little event for my server , here the event:




function EventFunction()
    local guards = ents.FindByClass("npc_antlionguard")
     if #guards >= 1 then return end
	 PrintMessage( HUD_PRINTCENTER, "An Antlion Guard has wondered into town, Be careful!" )	
	 PrintMessage( HUD_PRINTTALK, "An Antlion Guard has wondered into town, Be careful!" )	 
     local r = math.random( 1, 4 )
     local locs = { Vector(1423,736,-606), Vector(-1239,742,-606), Vector(-761,-1494,-606), Vector(1413,-1378,-606) }
     WorldSound( "tank.mp3", Vector( 0, 0, 0 ), 160, 100 )
     local ent = ents.Create("npc_antlionguard")
     ent:SetPos(locs[r])
     ent:Spawn()
end
timer.Create("eventtimer", 700, 0, EventFunction)



but now , i was thinking … HOW I CAN ADD SOMETHING TO CHECK WHEN IS GOING TO SPAWN THE ANTLION , like a timer in the clientside… :S

there’s any way that you can help me to make a countdown timer ? please ? like NWInt to use it on the CLientside please ?

Thanks ,

Schiaffino.

You could set a networked integer on all clients for how many seconds until the next event, but I don’t like networked variables, so here is some code that will network everything to the client. If you want me to show you how to do it with networked variables please ask.

I haven’t tested this code so it may contain a few syntax errors due to bad typing.

[lua]

local gEventDetails = {
[ “Wait” ] = 700,
};

if (SERVER) then

local EventDetails = {
	[ "NextEvent" ] = -1,
};

local function DoEvent ( ... )
	EventDetails.NextEvent = CurTime( ) + gEventDetails.Wait;

	local guards = ents.FindByClass("npc_antlionguard")
	if #guards >= 1 then return end
	PrintMessage( HUD_PRINTCENTER, "An Antlion Guard has wondered into town, Be careful!" )	
	PrintMessage( HUD_PRINTTALK, "An Antlion Guard has wondered into town, Be careful!" )	 
	local r = math.random( 1, 4 )
	local locs = { Vector(1423,736,-606), Vector(-1239,742,-606), Vector(-761,-1494,-606), Vector(1413,-1378,-606) }
	WorldSound( "tank.mp3", Vector( 0, 0, 0 ), 160, 100 )
	local ent = ents.Create("npc_antlionguard")
	ent:SetPos(locs[r])
	ent:Spawn()
end

local function SyncPlayerEventTime ( ply, ... )
	local TimeRemaining;
	
	if( EventDetails.NextEvent != -1 )then
		TimeRemaining = EventDetails.NextEvent - CurTime( ); // We're going to set TimeRemaing to be how many seconds until the next event
	else
		TimeRemaining = gEventDetails.Wait - CurTime( );
	end
	
	umsg.Start( "AntilionEventTimerSync", ply );
	umsg.Long( TimeRemaining );
	umsg.End( );
end

hook.Add( "PlayerInitialSpawn", "AntilionEventTimerSync", SyncPlayerEventTime );
timer.Create( "AntlionEventTimer", gEventDetails.Wait, 0, DoEvent );

else

local EventDetails = {
	[ "NextEvent" ] = 0,
};

function AntlionEventTimeRemaining( )
	return ( EventDetails.NextEvent - CurTime( ) );
end

local function SyncEvent ( UmData, ... )
	EventDetails.NextEvent = ( CurTime( ) + UmData:ReadLong( ) );
	
	timer.Simple( 3, function( )
		chat.AddText( "Next antlion event in " .. ( AntlionEventTimeRemaining() ) .. " seconds!" );
	end );
end

usermessage.Hook( "AntilionEventTimerSync", SyncEvent );

end
[/lua]