Jump to content

Timer


Bean666

Recommended Posts

Posted

Well im making an event it has a timer with a DX Text, but when a player joins, the timer resets for him , like a brand new timer. how can i save the timer duration to the new players that joined?

because basically i want this event , the timer for all players. same timer for all of them , any examples or instructions?

Example of how i mean:

i start resource

DX Text shows up:

Event ending in : 120 seconds

timer goes on example:

player ingame have 40 secs , all of them

but when a new player joins

his timer goes back to 120 seconds, how to save the timer for new players that joined?

Posted

i want a timer to save for a newly joined player,

because this script that i'm doing is a global event, there will be a DX Timer showed in each player's screen , example:

Event ending in : 120 seconds

then each tick - 1 seconds

but when there is a new player that joins the server his timer resets.

example: players that were ingame now 50 seconds

new player that joined : still 120 seconds.

i want the timer for all players the same. should i create a timeTable or smth

local sX,sY = guiGetScreenSize() 
local counter = 120 
local r,g,b = 0,255,0 
local redzone = createColRectangle ( -2978.78857, -806.87402, 1980, 2500 ) 
redzonemaparea = createRadarArea (-2978.78857, -806.87402, 1980, 2500, 255, 0, 0, 120) 
local timeTable = {}; 
  
function res(Res) 
outputChatBox("Event will end in 2 minutes!!", 255, 0, 0) 
addEventHandler("onClientRender",root,draw) 
end 
addEventHandler("onClientResourceStart", getRootElement(), res) 
  
function draw() 
    dxDrawText("Event Ending: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false) 
end 
removeEventHandler("onClientRender", root, draw) 
  
  
timer = setTimer(function() 
    counter = counter - 1 
    if counter < 0 then 
        counter = 120 
        r,g,b = 0,255,0 
    end 
    if counter <= 10 then 
        r,g,b = 255,0,0 
    end 
    if counter == 0 then 
    for i,v in ipairs(getElementsByType("player")) do  
    outputChatBox("IMPACT!", 255, 0, 0, false) 
    if isTimer(timer) then killTimer(timer) end 
    if isTimer(timer2)  then killTimer(timer2) end 
    end 
    end 
end,1000,0) 
  
timer2 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
outputChatBox("WARNING: Event ending!!", 255, 0, 0, false) 
end 
end 
end, 10000, 0) 
  
timer3 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
setElementHealth(player, 0) 
setWeather(19) 
if isTimer(timer2)  then killTimer(timer2) end 
end 
end 
end, 125000, 1) 
  
timer4 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
fadeCamera ( false, 1.0, 230, 178, 35 ) 
setTimer(fadeCamera, 3000, 1, true, 0.5) 
end 
end 
end, 122000, 1) 
  
timer5 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
sound = playSound("sound.mp3", false) 
if isElement(sound) then 
setSoundVolume(sound, 0.-- s8) -->
end 
end 
end, 109400, 1) 
  
timer6 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isTimer(timer3) then killTimer(timer3) end 
if isTimer(timer4) then killTimer(timer3) end 
if isTimer(timer5) then killTimer(timer3) end 
end 
end, 130000, 1) 

Posted (edited)

Timer in client is for local player, put it in server side and line 31 loop does not work cus there's no argument for 'visibleTo' in client side.

Edited by Guest
Posted (edited)

but how about the Countdown timer? the DX Text with the tick shall i make it stay in ClientSide?

EDIT: I found out about the dick

but when the player joins

the timers doesnt start for him now.

Edited by Guest
Posted

Use triggerClientEvent and send the count with it, before that must be used 'onClientResourceStart' and send trigger to server because the server works before the client.

Posted

I found out about the dick

but when the player joins

the timers doesnt start for him now. what functions should i use or what should i do?

the DX timer doesnt show up too.

local redzone = createColRectangle ( -2978.78857, -806.87402, 1980, 2500 ) 
redzonemaparea = createRadarArea (-2978.78857, -806.87402, 1980, 2500, 255, 0, 0, 120) 
  
function swag() 
for i, player in ipairs(getElementsByType("player")) do 
setTimer(triggerClientEvent, 500, 1, "dx", player) 
end 
end 
addEventHandler("onResourceStart", getRootElement(), swag) 
  
timer = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
triggerClientEvent(player, "timer", player) 
end 
end, 1000, 0) 
  
timer2 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
outputChatBox("WARNING: Leave San Fierro now!", player, 255, 0, 0, false) 
end 
end 
end, 10000, 0) 
  
timer3 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
setElementHealth(player, 0) 
setWeather(19) 
if isTimer(timer2)  then killTimer(timer2) end 
end 
end 
end, 125000, 1) 
  
timer4 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
fadeCamera ( player, false, 1.0, 230, 178, 35 ) 
setTimer(fadeCamera, 3000, 1, player, true, 0.5) 
end 
end 
end, 122000, 1) 
  
timer5 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
triggerClientEvent(player, "sound", player) 
end 
end, 109400, 1) 
  
timer6 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isTimer(timer3) then killTimer(timer3) end 
if isTimer(timer4) then killTimer(timer3) end 
if isTimer(timer5) then killTimer(timer3) end 
end 
end, 130000, 1) 
  
timer7 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
triggerClientEvent(player, "dxremove", player) 
end 
end, 126000, 1) 
  
timer8 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isTimer(timer) then killTimer(timer) 
end 
end 
end, 123000, 1) 
  

Posted

Client:

local sX,sY = guiGetScreenSize() 
local counter = 120 
local r,g,b = 0,255,0 
local redzone = createColRectangle ( -2978.78857, -806.87402, 1980, 2500 ) 
  
addEvent("dx",true) 
addEventHandler("dx", getRootElement(), 
function () 
addEventHandler("onClientRender", root, draw) 
end 
) 
  
addEvent("dxremove", true) 
addEventHandler("dxremove", getRootElement(), 
function () 
removeEventHandler("onClientRender", root, draw) 
end 
) 
  
addEvent("sound",true) 
addEventHandler("sound", getRootElement(), 
function () 
sound = playSound("sound.mp3", false) 
if isElement(sound) then 
setSoundVolume(sound, 0.-- s8) -->
end 
end 
) 
  
function draw() 
    dxDrawText("Time left: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false) 
end 
removeEventHandler("onClientRender", root, draw) 
  
addEvent("timer", true) 
addEventHandler("timer", getRootElement(), 
function () 
    counter = counter - 1 
    if counter < 0 then 
        counter = 120 
        r,g,b = 0,255,0 
    end 
    if counter <= 10 then 
        r,g,b = 255,0,0 
    end 
    if counter == 0 then 
    for i,v in ipairs(getElementsByType("player")) do  
    outputChatBox("IMPACT!", 255, 0, 0, false) 
    end 
    end 
end 
) 
  
function notcolshape( player ) 
if (player == localPlayer) then  
local id = getWeather() 
if id == 19 then 
fadeCamera( false, 0.4, 0, 0, 0 ) 
setTimer(fadeCamera, 2000, 1.0, true) 
setTimer(setWeather, 500, 1, 7) 
end 
end 
end 
addEventHandler ( "onClientColShapeLeave", redzone, notcolshape ) 
  
function redzoneEnter ( player ) 
if (player == localPlayer) then 
fadeCamera( false, 0.4, 0, 0, 0 ) 
setTimer(fadeCamera, 2000, 1.0, true) 
setTimer(setWeather, 500, 1, 19) 
end 
end 
  
setTimer(function () addEventHandler("onClientColShapeHit", root, redzoneEnter) outputChatBox("San Fierro",255,0,0) end,126000,1, true) 

server:

local redzone = createColRectangle ( -2978.78857, -806.87402, 1980, 2500 ) 
redzonemaparea = createRadarArea (-2978.78857, -806.87402, 1980, 2500, 255, 0, 0, 120) 
  
function swag() 
for i, player in ipairs(getElementsByType("player")) do 
setTimer(triggerClientEvent, 500, 1, "dx", player) 
end 
end 
addEventHandler("onResourceStart", getRootElement(), swag) 
  
timer = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
triggerClientEvent(player, "timer", player) 
end 
end, 1000, 0) 
  
timer2 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
outputChatBox("WARNING: Leave San Fierro now!", player, 255, 0, 0, false) 
end 
end 
end, 10000, 0) 
  
timer3 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
setElementHealth(player, 0) 
setWeather(19) 
if isTimer(timer2)  then killTimer(timer2) end 
end 
end 
end, 125000, 1) 
  
timer4 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isElementWithinColShape(player, redzone) then 
fadeCamera ( player, false, 1.0, 230, 178, 35 ) 
setTimer(fadeCamera, 3000, 1, player, true, 0.5) 
end 
end 
end, 122000, 1) 
  
timer5 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
triggerClientEvent(player, "sound", player) 
end 
end, 109400, 1) 
  
timer6 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isTimer(timer3) then killTimer(timer3) end 
if isTimer(timer4) then killTimer(timer3) end 
if isTimer(timer5) then killTimer(timer3) end 
end 
end, 130000, 1) 
  
timer7 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
triggerClientEvent(player, "dxremove", player) 
end 
end, 126000, 1) 
  
timer8 = setTimer(function() 
for i, player in ipairs(getElementsByType("player")) do 
if isTimer(timer) then killTimer(timer) 
end 
end 
end, 123000, 1) 

Posted

You can't use 'onResourceStart' and send trigger to client because the client are not downloaded yet, do it like this ex :

local counter = 120 
addEventHandler ( 'onClientResourceStart', resourceRoot, function ()  
    triggerServerEvent ( ... ) -- complete it. 
end) 
  
function draw() 
    dxDrawText("Event Ending: "..counter ,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false) 
end 
addEventHandler("onClientRender",root,draw) 
  
addEvent (...)  -- complete it. 
addEventHandler ( 'trigger', root,function ( count ) 
    counter = count 
end) 

 counter = 120 
addEvent ( ... ) -- complete it. 
addEventHandler ( ... -- complete it. 
setTimer ( function() 
    counter = counter - 1 
        triggerClientEvent ( ... ) -- complete it and send the counter var with it. 
end,...) -- complete it. 
end) 
  

i'm sorry i'm too lazy :lol: .

Posted

is that all? im kinda lazy af too haha tired af still didn't sleep, btw is that all?

could you mix that code with the original code please?( i mean to make sure if thats all or what )

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...