Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make my metronome function time correctly?

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?

1 answer

I have the same problem, But! You can use local script instead.

Server:

local sound = script.SoundName --Metronome tick
local button = script.Parent.Button --GUI Button
local Event = game.ReplicatedStorage.RemoteEvent -- Remote Event
local BPM = 0.5 -- 120 BPM (60/120)

local function startMetronome()
	Event:FireAllClients(sound, BPM)
end

button.MouseButton1Click:Connect(startMetronome)

Local:

local Event = game.ReplicatedStorage.RemoteEvent

Event.OnClientEvent:Connect(function(sound, BeatsPerMinute)
	while true do
		sound:Play()
		wait(BeatsPerMinute)
	end
end)

3 comments

0

Won't that produce the same result since in the end it's doing the same operation?

0

Do you use local script?

0

Yes, sorry. I forgot to mention at the time my original issue and measurement (using tick()) were both inside of a local script.

This discussion is preserved as it was. New answers, comments, and votes are closed.