'Logitech LUA Script Delay in activation

I am currently using this script; however, sometimes, it doesn't activate during fast gameplay like when clicking the right mouse and left mouse buttons very fast and sometimes randomly.

EnablePrimaryMouseButtonEvents  (true)
function OnEvent(event,arg)
    if IsKeyLockOn("scrolllock")then
        if IsMouseButtonPressed(3)then
            repeat
                if IsMouseButtonPressed(1) then
                    repeat
                        MoveMouseRelative(0,16)
                        Sleep(1)
                    until not IsMouseButtonPressed(1)
                end
            until not IsMouseButtonPressed(3)
        end
    end
end

Is there any way to fix this issue and make the script work good?
Thank you in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source