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

How to read the TimeLength property of a Sound object in online mode?

I've written a script that randomly picks a SoundId from a table and plays the sound. I've also included a function to fade the sound out when the song is finished by reading the TimeLength property from the sound and subtracting 3 seconds and waiting that long before fading the sound out. This works as intended within studio test mode but fails to read the TimeLength property during online play. The songs start just fine when online but they immediately fade out and the output shows the TimeLength as being 0.

The code that works in Studio but not Online:

math.randomseed(tick()) --Making the randomizer more random
-------------------------------------------------
local song1 = 240288382 --I have the links to the songs commented beside each variable
local song2 = 165053397
local song3 = 170784240
local song4 = 256930014
--...this goes on up to 22 songs so far
-------------------------------------------------
local music = Instance.new("Sound") 
music.Name = "Song"
music.Looped = false
music.Volume = 1
music.Parent = script.SoundHandler --Folder Object
-------------------------------------------------

--Preloading the songs--
-------------------------------------------------
local Assets = {song1,song2,song3,song4}
 for _, asset in ipairs(Assets) do
     game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset)
end
-------------------------------------------------
function SongRandomize() 
	music.SoundId = "http://www.roblox.com/asset/?id=" ..Assets[math.random(1,#Assets)]
	wait(2)
	local duration = music.TimeLength
	local waitTime = duration - 3
	print("Song Randomized")
	print("Song Chosen = " ..music.SoundId)
	print("Song duration = " ..duration)
	print("waitTime = " ..waitTime)
	wait(1)
	music:Play()
	wait(waitTime)
	FadeOut()
	music:Stop()
	wait(1)
end

function FadeOut() --Fade the music out instead of an abrupt stop
	local volume = music.Volume
	print("Fading song out")
	for i = 0,100 do
		volume = volume - 0.01
		music.Volume = volume
		wait(0.03)
	end
end

I have tried creating a new sound object each time SongRandomize() is called and calling :Destroy() on it at the end of the function, which had the same result of fading out after 0 seconds. I also tried creating a sound object for each variable like so:

local song1 = Instance.new("Sound")
	song1.Looped = false
	song1.Volume = 1
	song1.Parent = script.SoundHandler
	song1.SoundId = "http://www.roblox.com/asset/?id=240288382"

local song2 = Instance.new("Sound")
	song2.Looped = false
	song2.Volume = 1
	song2.Parent = script.SoundHandler
	song2.SoundId = "http://www.roblox.com/asset/?id=165053397"
--...and so forth

with the same result of local duration = music.TimeLength returning 0.

Any ideas on how I can make this work? I would really like to keep the fade function but it's being the problem at the moment. Thanks in advance!

1 answer

✓ Accepted answer

You are not allowed to find the TimeLength of a song on the server side in online mode. You will have to ping a player for how long the time is.

1 comment

0

Thanks! I have no idea how to do that but it is good to know.

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