Timer in Lua [Need assistance]

I’m wondering how would I go about adding a timer in Lua,

I want to add this to /hood, how would I set the event as a command with this

Citizen.CreateThread(function()
    local time = 5
    while (time ~= 0) do
        Wait( 1000 )
        time = time - 1
    end
    TriggerClientEvent("hood")
end)

How about something like this:

timera = 0

function SetTimera(a)
	timera = GetGameTimer() - a
end

function GetTimera()
	return GetGameTimer() - timera
end

edit:

Would use it the following way:

Citizen.CreateThread(function()
  Citizen.Wait(0)
   SetTimera(0)
   local triggered = false
   while not triggered do
       if GetTimera() > 5000 then TriggerEvent("hood") triggered = true end
   end
end)

The timer itself works, but I’m trying to set it so the timer is activated so when a user does /hood it makes them wait 5 seconds, I’m trying to figure out how to add the timer to the chat message /hood

Or something Like this

Citizen.CreateThread(function(timer)
    local time = 5
    while (time ~= 0) do
        Wait( 1000 )
        time = time - 1
    end

AddEventHandler( 'chatMessage', function( source, n, message )
    if ( message == "/hood" ) then 
    	CancelEvent()
        TriggerClientEvent( 'hood', source, timer )
    end 
end )

Would that work?

while (time > 0) do is better.

I misunderstood you. You should specify. What you want, like /hood 5 for something to happen in 5s?

Basically what I’m trying to do is if the message /hood is entered wait 5 seconds before triggering the event.

local timer = 0

Citizen.CreateThread(function(timer)
    while timer > 0 do
        Wait(1000)
        timer = timer - 1
    end
end)

AddEventHandler( 'chatMessage', function(source, n, message)
    if (message == "/hood" ) then 
    	if timer > 0 then
            CancelEvent()
        else
            TriggerClientEvent('hood', source, timer)
        end
    end 
end)

I’l give that a shot now cheers.

1 Like
AddEventHandler( 'chatMessage', function(source, n, message)
    if (message == "/hood" ) then 
    	Citizen.Wait(5000)
        TriggerClientEvent('hood', source)
    end 
end)

Why would you need a timer?

I’m doing it for roleplay purposes so it doesn’t just open instantly.

Wait you just want to have a delay on the command?

Then just use the command he gave you. I before misunderstood you.

Yeah sorry. Alright thanks mate.

1 Like

Send the code where you have the delay too please.

RegisterNetEvent( 'drawNotification' )
AddEventHandler( 'drawNotification', function()
    drawNotification("~g~Your vehicle is being repaired by the mechanic. Please wait 15 seconds.")end)
end)

SERVER:
AddEventHandler( 'chatMessage', function(source, n, message)
    if (message == "/trunk" ) then 
        TriggerClientEvent('drawNotification', source)
    	Citizen.Wait(15000)
        TriggerClientEvent('fixVehicle ', source)
    end 
end)

This is for the client. Sorry I wrote this on my phone

RegisterCommand("hood", function(args, string)
     Citizen.Wait(5000)
     TriggerEvent("eventname")
end, false)
2 Likes

Dat is da best wae to go