Jump to content

what's wrong with my timer


mores3

Recommended Posts

Hi 

 

Someguys know me, but this time I will try do something. can you tell me what's wrong with this code and how to solve it ? 

 

 

working - not resers/disappears when map is loading

local sX,sY = guiGetScreenSize() 
local counter = 15 
local r,g,b = 0,255,0 
  
function drawCounterSHI() 
    dxDrawText("Your vehicle will change in: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false) 
end 
  
function startEverything() 
addEventHandler("onClientRender",root,drawCounterSHI) 
setTimer(function() 
    counter = counter - 1 
    if counter < 1 then 
        counter = 15 
        r,g,b = 0,255,0 
    end  
end,1000,0) 
  
end 
addEvent("onCounterStart",true) 
addEventHandler("onCounterStart",getRootElement(),startEverything) 

 

 

dont working after my changes  (i tried use racestates)

 

local sX,sY = guiGetScreenSize() 
local counter = 15 
local r,g,b = 0,255,0 
  
function drawCounterSHI() 
    dxDrawText("Your vehicle will change in: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false) 
end 
  
function startEverything() 
addEventHandler("onClientRender",root,drawCounterSHI) 
setTimer(function() 
    counter = counter - 1 
    if counter < 1 then  
end,1000,0) 

removeEventHandler("onClientRender", root, draw) 
if(newState == "LoadingMap") then  killTimer(timer) 
   end 
  
end 
addEvent("onCounterStart",true) 
addEventHandler("onCounterStart",getRootElement(),startEverything) 

 

Link to comment

This is not code lol. Check the errors in the console. There must be errors since there are some undefined variables:

draw (it should be a function)

timer (it should be a timer)

newState (it should be a string?)

Before i said this is not code because the if at line 13 is not closed (well, it get closed actually... but this leave open other things)

I would sugget to use indentation so you can visually know where there are these kind of errors

local sX,sY = guiGetScreenSize()
local counter = 15
local r,g,b = 0,255,0

addEvent( "onCounterStart", true )

function drawCounterSHI()
	dxDrawText("Your vehicle will change in: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false)
end

function startEverything()

	addEventHandler( "onClientRender", root, drawCounterSHI )
	removeEventHandler("onClientRender", root, draw) --define draw

	setTimer( --someTimer?
		function()
			counter = counter - 1
			if counter < 1 then
				--counter = 15 --???
			end--this was missing
		end,
	1000, 0 )

	if newState == "LoadingMap" then --define newState. Is it derived from "racestates"???
		killTimer(timer) --define timer. Is it the timer "someTimer"?
	end
end
addEventHandler( "onCounterStart", root, startEverything )

 

3 hours ago, mores3 said:

(i tried use racestates)

Can you link? i can't find it

Edited by LoPollo
Link to comment

I haven't understood  everything :( 

 

I've done it , and I dont know what exactly mean define what you mean by this word ? 

local sX,sY = guiGetScreenSize()
local counter = 15
local r,g,b = 0,255,0

addEvent( "onCounterStart", true )

function drawCounterSHI()
	dxDrawText("Your vehicle will change in: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false)
end

function startEverything()

	addEventHandler( "onClientRender", root, drawCounterSHI )
	removeEventHandler("onClientRender", root, draw) --define draw

	setTimer( --someTimer?
		function()
			counter = counter - 1
			if counter < 1 then
			counter = 15 
			r,g,b = 0,255,0 
                    end
		end,
	1000, 0 )

	if newState == "LoadingMap" then --define newState. Is it derived from "racestates"???
		killTimer(timer) --define timer. Is it the timer "someTimer"?
	end
end
addEventHandler( "onCounterStart", root, startEverything )

 

Link to comment
2 hours ago, mores3 said:

I haven't understood  everything :( 

 

I've done it , and I dont know what exactly mean define what you mean by this word ? 


local sX,sY = guiGetScreenSize()
local counter = 15
local r,g,b = 0,255,0

addEvent( "onCounterStart", true )

function drawCounterSHI()
	dxDrawText("Your vehicle will change in: "..counter,0,sY*0.8,sX,sY,tocolor(r,g,b,255),2,"default","center","top",false,false,false)
end

function startEverything()

	addEventHandler( "onClientRender", root, drawCounterSHI )
	removeEventHandler("onClientRender", root, draw) --define draw

	setTimer( --someTimer?
		function()
			counter = counter - 1
			if counter < 1 then
			counter = 15 
			r,g,b = 0,255,0 
                    end
		end,
	1000, 0 )

	if newState == "LoadingMap" then --define newState. Is it derived from "racestates"???
		killTimer(timer) --define timer. Is it the timer "someTimer"?
	end
end
addEventHandler( "onCounterStart", root, startEverything )

 

Always when you define variables, you define them like this in Lua:

variable = something -- timer = setTimer(...) etc.

So, your timer is never defined in a variable, so calling a variable called timer would do nothing...

Same problem with newState, where is it defined? How can it know what it is, if you never define what it is?

Edited by myonlake
Link to comment

define means to give it a name. for example;

 

your code needs to use getPlayerName(thePlayer) 10 times. Instead of writing this 10 times (especially with longer lines it's better) you can give it a 'name'.

name = getPlayerName(thePlayer) name is now defined as the result of getPlayerName (which returns a name as a string or false value)

Another little example:

outputChatBox(number) --number doesn't exist as a name, it's unDEFINED

number = 7
outputChatBox(number) --outputs: 7

 

Hope this helps

Link to comment

onRaceStateChanging will be called on race end - map change, i think. Use this to stop and reset the timer and stop drawing the text.

 

15 hours ago, ViRuZGamiing said:

Kill the timer when you die, create it when you spawn.

Even if this wasn't the answer to you prevoious question, it's a good idea to reset the timer here too, since (i guess) the timer which will make you change vehicle get resetted too (stopped on death, resetted and started on spawn)-

Link to comment

i don't understant any of your latest replies... can you explain? sorry for this
 

24 minutes ago, mores3 said:

in my view only numbers will be enough 

only numbers to do what?

 

24 minutes ago, mores3 said:

What function should I use

to stop/reset/start the timer? isTimerkillTimer, setTimer are enough if that's the answer

EDIT: remember to stop/start showing the text, adding or removing the onClientRender handler

 

1 hour ago, mores3 said:

And there start "stairs"

what do you mean? that from now it will not be easy?

 

I feel dumb :|

Edited by LoPollo
Link to comment

well if you have these example events that supposable would be triggered on the described moment:

 

local vehicleIDS = {
    602, 545, 496, 517, 401, 410, 518, 600, 527, 436, 589, 580, 419, 439, 533, 549, 526, 491, 474, 445, 467, 604, 426, 507, 547, 585, 405, 587,
    409, 466, 550, 492, 566, 546, 540, 551, 421, 516, 529, 592, 553, 577, 488, 511, 497, 548, 563, 512, 476, 593, 447, 425, 519, 520, 460, 417,
    469, 487, 513, 581, 510, 509, 522, 481, 461, 462, 448, 521, 468, 463, 586, 472, 473, 493, 595, 484, 430, 453, 452, 446, 454, 485, 552, 431,
    438, 437, 574, 420, 525, 408, 416, 596, 433, 597, 427, 599, 490, 432, 528, 601, 407, 428, 544, 523, 470, 598, 499, 588, 609, 403, 498, 514,
    524, 423, 532, 414, 578, 443, 486, 515, 406, 531, 573, 456, 455, 459, 543, 422, 583, 482, 478, 605, 554, 530, 418, 572, 582, 413, 440, 536,
    575, 534, 567, 535, 576, 412, 402, 542, 603, 475, 449, 537, 538, 570, 441, 464, 501, 465, 564, 568, 557, 424, 471, 504, 495, 457, 539, 483,
    508, 571, 500, 444, 556, 429, 411, 541, 559, 415, 561, 480, 560, 562, 506, 565, 451, 434, 558, 494, 555, 502, 477, 503, 579, 400, 404, 489,
    505, 479, 442, 458, 606, 607, 610, 590, 569, 611, 584, 608, 435, 450, 591, 594
}

local carTimer

//this might be a real event, haven't checked I don't do race stuff usually
addEventHandler("onPlayerRaceSpawn", getRootElement(), function()
      if (isTimer(carTimer)) then killTimer(carTimer) end
      carTimer = setTimer(function()
          --also suppose that source is the driver
          setVehicleModel(getPedOccupiedVehicle(source), vehicleIDS[math.random(0, #vehicleIDS)])
      end, 15000, 0)
end)
    
addEventHandler("onPlayerRaceWasted", getRootElement(), function()
	if (isTimer(carTimer)) then killTimer(carTimer) end
end)

 

Link to comment

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...