PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

TimeLength

Sound

double

The length of the Sound in seconds. If the Sound is not loaded, this value will be 0.

This property is often used in conjunction with Sound/PlaybackSpeed to adjust the speed of a sound so it lasts for a specific duration (see examples). When Sound/PlaybackSpeed is equal to 1, the sound will take TimeLength seconds to complete.


Code Samples


Play a Sound for a Specific Duration

This code sample includes a simple function that uses Sound.TimeLength and Sound.PlaybackSpeed to play a sound that’ll take the given duration to complete. It achieves this by setting the PlaybackSpeed of the sound to be equal to the TimeLength of the sound divided by the desired duration.

Note that as TimeLength is equal to 0 when the sound has not loaded, the function will yield whilst it loads the sound.

local function playForDuration(sound, duration)
	if not sound.IsLoaded then
		sound.Loaded:wait()
	end
	local speed = sound.TimeLength / duration
	sound.PlaybackSpeed = speed 
	sound:Play()
end