Jump to content

Killing timers in a table


Overkillz

Recommended Posts

Hello dear community. Today I'm having an issue storing some timers in a table and later kill them.

I have tried several ways to reach my goal, but it doesn't work at all. My goal is to delete all the timers in a row without using a vairable and later add it on a table. Just using a similar way to the following one

Note: I already note that a table with a name like this: tableName["Whatever"] is a personal table. But as I told before, I want to use a similar way to delete all the timers or get them from the table arenaSrv.timers. And no, I won't use getTimers() to delete the whole timers.

function resetArenaTimer(timerName)
	if isTimer(arenaSrv.timers[timerName]) then
		killTimer(arenaSrv.timers[timerName])
		arenaSrv.timers[timerName] = nil
	end
end

if not arenaSrv.timers["readyWarn"] then
	arenaSrv.timers["readyWarn"] = setTimer(resetArenaTimer,15000,1,arenaSrv.timers["readyWarn"])
end

-- If all the players are ready before the 15 seconds then kill the timer
	resetArenaTimer(arenaSrv.timers["readyWarn"])
-- Lets add 3 timers
	arenaSrv.timers["Countdown2"] = setTimer ( triggerClientEvent, 1000, 1, player,"onTestingCD",player,"2")
	arenaSrv.timers["Countdown1"] = setTimer ( triggerClientEvent, 2000, 1, player,"onTestingCD",player,"1")
	arenaSrv.timers["Countdown0"] = setTimer ( triggerClientEvent, 3000, 1, player,"onTestingCD",player,"GO!")
	
function clearTimersTable() --THIS FUNCTION IS EXECUTED BY A COMMAND BUT I DIDNT PUT IT
	for i, timer in ipairs(arenaSrv.timers) do
		resetArenaTimer(timer) 
	end
end

Thanks for reading. Regards.

Edited by Overkillz
Link to comment
12 minutes ago, #َxLysandeR said:

function clearTimersTable() --THIS FUNCTION IS EXECUTED BY A COMMAND BUT I DIDNT PUT IT
	for index,value in pairs(arenaSrv.timers) do
		resetArenaTimer(index)
	end
end

 

It didn't work.

I'm still not understanding why even a simpler code doesn't work. Check it out. It still printing those 3 timers in from the table.

arenaSrv.timers.cd2 = setTimer ( triggerClientEvent, 1000, 1, player,"onTestingCD",player,"2")
arenaSrv.timers.cd1 = setTimer ( triggerClientEvent, 2000, 1, player,"onTestingCD",player,"1")
arenaSrv.timers.cd0 = setTimer ( triggerClientEvent, 3000, 1, player,"onTestingCD",player,"GO!")

function destroyTimers()
	for i, theTimer in ipairs(arenaSrv.timers) do
		if isTimer(theTimer) then
			killTimer(theTimer)
			theTimer = nil
		end
	end
	iprint(arenaSrv.timers)
end
addCommandHandler("g",destroyTimers)

 

Link to comment
3 minutes ago, Master_MTA said:

who is the player @_@

you even didn't define it before

 

It is defined, I just didn't post it ..

for i,player in ipairs(arenaSrv.players) do 
arenaSrv.timers.cd2 = setTimer ( triggerClientEvent, 1000, 1, player,"onTestingCD",player,"2")
arenaSrv.timers.cd1 = setTimer ( triggerClientEvent, 2000, 1, player,"onTestingCD",player,"1")
arenaSrv.timers.cd0 = setTimer ( triggerClientEvent, 3000, 1, player,"onTestingCD",player,"GO!")
end

There is not a debugscript3 error. It is an issue with the table ... I would already realized about it ...

Link to comment

Well, I think I have found the problem. I guess I haven't got any issue the whole time.

Why ?

I thought that killing the timer and setting the variable as nil it would dissapear from the table, but, no, it still there with the name: userdata*x*****.

Im still not understading why it doesn't dissapear from the table setting the variable as nil.

Anyways, thanks for the help, if someone could argument why it doesn't dissapear would be great.

Regards.

Link to comment
  • Moderators
11 hours ago, Overkillz said:

Well, I think I have found the problem. I guess I haven't got any issue the whole time.

Why ?

I thought that killing the timer and setting the variable as nil it would dissapear from the table, but, no, it still there with the name: userdata*x*****.

Im still not understading why it doesn't dissapear from the table setting the variable as nil.

Anyways, thanks for the help, if someone could argument why it doesn't dissapear would be great.

Regards.

 

arenaSrv.timers[i] = nil 

-- OR


-- outside of the loop

arenaSrv.timers = {} -- reset the table

You have remove it from the table. Nilling a variable is not the same.

  • Like 1
Link to comment

I can suggest you to not use few timers, but one timer with 1 sec duration and multiple times to execute and some if's inside function.
 

local timerN = 0

function someFunction()
  timerN = timerN + 1
  if timerN == 1 then
 	 print("3")
  elseif timerN == 2 then
	 print("2")
  elseif timerN == 3 then
	 print("1")
  elseif timerN == 3 then
	 print("GO")
	 timerN = 0
  end
end

setTimer(someFunction, 1000, 4)

 

  • Like 1
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...