While will this loop only execute ONCE and after wont repeat

Loop

  • Im trying to trigger it when im in a specific speed zone, when in the zone im displaying a notification and i want (in the repeat section) to keep increasing the counter + 1 while im in the zone.

  • Problem: the loop triggers once and never again even when falling to 0 speed or leaving car etc.

please help :slight_smile:

Your repeat until is the issue. Your variable speed is set before the loop but is then never updated; it would forever be one value. You would need something like this:

local speed = GetEntitySpeed(vehicle) * 2.236936

if speed >= 20 and speed <= 59 then
    notify(Config.Messages.xp1)
    notify(Config.Messages.xpc1)
    repeat
        Config.xpTotal = Config.xpTotal + 1
    until GetEntitySpeed(vehicle) * 2.236936 <= 19 or GetEntitySpeed(vehicle) * 2.236936 >= 60
end

This, however, is not very efficient as you have to hit the native twice per loop, since LUA doesnโ€™t support variables being set inside if statements. An alternative could look like this:

local speed = GetEntitySpeed(vehicle) * 2.236936

if speed >= 20 and speed <= 59 then
    notify(Config.Messages.xp1)
    notify(Config.Messages.xpc1)

    while (speed >= 20 and speed <= 59) do
        Config.xpTotal = Config.xpTotal + 1

        speed = GetEntitySpeed(vehicle) * 2.236936

        Citizen.Wait(0)
    end
end

Note I have not tested any of this code, use at your own risk.

Tried it the logic is far better than mine but still provides the same outcome ive closed off this idea for now thanks for the help though :slight_smile: