So . . . I tried to search this up and had no luck. How would I make a timer for my rhythm game.
How would I do a timer that is in sync with the audio file that is playing in the background? I want to put it on GUI and I have that set up.
I want to make a timer in the format “TIMENOW/ENTIRETIME”
For example, if I had been playing for a minute and the song was three minutes long, it would look like “1:00/3:00”
Thanks DevForum!
Each Sound
instance has a TimeLength and TimePosition property. The first specifies how long the sound file is in seconds, and the second at what second the sound is while being played.
Of course, you’d have to format the timer to turn from just seconds to seconds/minutes.
Sounds fun. Here’s a bit of code I typed up that should work.
function Format(Int)
return string.format("%02i", Int)
end
function convertToHMS(Seconds)
local Minutes = (Seconds - Seconds%60)/60
Seconds = Seconds - Minutes*60
local Hours = (Minutes - Minutes%60)/60
Minutes = Minutes - Hours*60
return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end
Tested examples for proof:
convertToHMS(1) -> “00:00:01”
convertToHMS(59) -> “00:00:59”
convertToHMS(60) -> “00:01:00”
conve…
^ Just get rid of the hours and this should work fine.
So just use that and just get the time of the song that’s playing? is it possible to get the time of a song?
Then I suppose all I need to do is that, and just put that on my timer label since the song IS already playing
Qinrir
(Quin)
May 19, 2023, 3:25pm
#6
mhm thats all you need to do!
1 Like
local timerLabel = script.Parent
local runserv = game:GetService("RunService")
runserv.Heartbeat:Connect(function()
local song = game.ReplicatedStorage.chosenSong.Value
local timer = (song.TimePosition .. "/" .. song.TimeLength)
timerLabel.Text = timer
end)
It’s not seeming to change for some reason? Is it possibly because I put the chosenSong
value in ReplicatedStorage
?
Qinrir
(Quin)
May 19, 2023, 3:34pm
#8
wait what chosenSong.Value
? you need the actual sound Instance for that property not just a number value. or is that an object value?
It is an object value, it’s so that when I use teleport service players can pick the song and it will choose the correct object value.
Edit: It’s working, I moved it out of the workspace, now I just need to round it.
EDIT 2: It works well, but the numbers are glitching a little.
Qinrir
(Quin)
May 19, 2023, 3:44pm
#10
maybe try using .changed then? maybe that would work?
local timerLabel = script.Parent
local runserv = game:GetService("RunService")
local songval = game.ReplicatedStorage.chosenSong
songval.Changed:Connect(function()
local song = game.ReplicatedStorage.chosenSong.Value
song.Changed:Connect(function()
local timer = (song.TimePosition .. "/" .. song.TimeLength)
timerLabel.Text = timer
end)
end)
or maybe just use a sound instance and NOT a value. can you also tell me where the song is? and if it even is playing? on the client
system
(system)
Closed
June 2, 2023, 3:44pm
#11
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.