[HELP] IsControlPressed every tick fires more than once

When using IsControlPressed ive noticed it will capture the event more than once when checking every tick.

Citizen.CreateThread(function()
        local times_pressed = 0
	while true do
                Citizen.Wait(0)
                if isControlPressed(0, 206) then --Btn "E" Pressed
                        times_pressed = times_pressed + 1
                        print(times_pressed)
                end
        end
end)

I found the easiest fix is to increase the wait time to lower the chance of double counting the press of a key. I easily get times_pressed up to 5 by pressing the key once when accessed every tick but increasing the wait could mean missing the event. Also the amount to wait would vary from computer to computer since not all computers are the same.

Looking for best practice here. How are people using this to not execute their code more than once? Additional Flag checks? There is potential for a lot of uneeded execution time for using this native it seems.

Use IsControlJustPressed instead

1 Like

Do you recommend every tick?

Yes, otherwise you have to be more precise as a user and may sometimes not get a response because you are pressing the key just when the tick is waiting.

1 Like

IsControlPressed checks if the player is HOLDING the button, it will register multiple times per second since the key will be pressed every tick.

U can also do this

Citizen.CreateThread(function()
    while true do
        if IsControlPressed(0, key) then
            dosomething()

            while IsControlPressed(0, key) do
                Citizen.Wait(0)
            end
        end
        Citizen.Wait(50)
    end
end)