Hello!
Recently, I wanted to make a little karaoke in my game.
It would work like this: -The script notices that a certain piece of music is playing
-The script changes the text of the Label to the lyric of a specific passage that I have rewritten for it
Today I would like to focus on the bold part of the text.
I have a problem to make a handling eventthat will activate when a given piece of music is playing. How can I do that?
I have tried to do this with TimePosition before, but this function did not work for me or I used it
incorrectly.
GetPropertyChangedSignal won’t fire for a ‘Sound’ object’s ‘TimePosition’ property since it’s updated by the engine at a frequency greater than once per frame, you can however use a ‘RunService’ event loop to query the ‘Sound’ object’s ‘TimePosition’ property every frame.
local Game = game
local RunService = Game:GetService("RunService")
local Sound = nil --Reference to 'Sound' object.
local Connection
local function OnRenderStep()
if Sound.TimePosition > Sound.TimeLength / 2 then --If audio track has reached its midpoint.
Connection:Disconnect() --Disconnect event.
--Code.
end
end
local function OnSoundPlayed()
if Connection and Connection.Connected then Connection:Disconnect() end --Prevents connection stacking.
Connection = RunService.RenderStepped:Connect(OnRenderStep)
end
Sound.Played:Connect(OnSoundPlayed)