Hello, I'm fairly new to programming and Lua in general. I've been having issues with the timing of my metronome function.
Here's an example:
local sound = script.SoundName --Metronome tick
local button = script.Parent.Button --GUI Button
local BPM = 0.5 -- 120 BPM (60/120)
local function startMetronome()
while true do
sound:Play()
wait(BPM)
end
end
button.MouseButton1Click:Connect(startMetronome)
This is a very simple version of my script but even at this level I encounter the same issue. No matter what, it always ends up out of sync (timing it with music, other metronomes, etc). I figure this is because of the code itself and the small differences in the timing it takes to play the sound and wait in the loop? In the end I'm not sure. Is there a way to accurately play this sound in exact intervals?
How do other programs manage this?

3 comments
Won't that produce the same result since in the end it's doing the same operation?
Do you use local script?
Yes, sorry. I forgot to mention at the time my original issue and measurement (using tick()) were both inside of a local script.