I might make a little function called init_beat() that does this, so you donāt have to repeat it in both functions.
if pattern == 5 then
pattern = 1
end
this could be changed to
if pattern = #beat + 1 then pattern = 1 end
or
pattern = pattern % #beat + 1
If you use #beat instead of hardcoding the 5, it should always work if the beat length changes so you donāt have to update that number.
You could also pass an optional maxBeatLength param to drum_loop, like
function drum_loop(count, maxBeatLength)
maxBeatLength = maxBeatLength or 8
and change if beatReset == 8 to if beatReset == maxBeatLength
This way it would default to 8 but be customizable if the script author wants to pass in a different number.