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

TimeLength script not working?

I'm trying to get this to play another song when the first song ends, but I keep getting "ending song" spammed in the output.

repeat wait() until script.Ignore
--Playlist
Songs = {234174567,245311593,175588117,152208064} --there are more songs but too many for sh
--Variables
local Audio = script.Parent.Speakers:GetChildren()
local cont = false
mainspeaker = script.Parent.Speakers.MainSpeaker.Sound

while true do
	local randomize = Songs[math.random(1, #Songs)]
	cont = false
--	script.Ignore.Previous.Value = Audio.MainSpeaker.Sound.SoundId
	for i = 1, #Audio do
		Audio[i].Sound.SoundId = "rbxassetid://"..randomize
	end
	if mainspeaker.SoundId == script.Ignore.Previous.Value then
		repeat wait(.5) mainspeaker.SoundId = "rbxassetid://"..randomize until mainspeaker.SoundId ~= script.Ignore.Previous.Value
	end
	for i = 1, #Audio do
		Audio[i].Sound:Play()
	end
	for i = 1, #Audio do
		for c = 0,1,.1 do
			Audio[i].Sound.Volume = c
		end
	end
	cont = true
	local tim = mainspeaker.TimeLength - 5
	wait(tim)
	print("ending song")
	for i = 1, #Audio do
		for c = 1,0,-.1 do
			Audio[i].Sound.Volume = c
		end
	end
end

1 answer

There is somewhat of a delay with roblox whenever you change the SoundId of a sound, which causes the time length to not immediately change. Try adding a wait() or wait(.1) before the line where you use the TimeLength property.

If this fails, you'll have to either wait a little more or possibly just resort to using a dictionary (a table that contains each song id as an index with its value as the length in seconds).

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