Music Script Not Functioning

Hi, I’m trying to create a script that checks the TimePosition and TimeLength of a sound, and if they are equal to each other, it will update multiple sounds in a folder to a new SoundId.

However, this doesn’t seem to work for me. The Print(song) command in the script seems to work, as it outputs a new ID every time the while loop is ran, however, the sound simply stops once the end of the song is reached.

The speakers (as found in Workspace):

image

local songs = {"http://www.roblox.com/asset/?id=xxxxxxx", "http://www.roblox.com/asset/?id=xxxxxxx", "http://www.roblox.com/asset/?id=xxxxxxx", "http://www.roblox.com/asset/?id=xxxxxxx", "http://www.roblox.com/asset/?id=xxxxxxx", "http://www.roblox.com/asset/?id=xxxxxxx", "http://www.roblox.com/asset/?id=xxxxxxx"}

local check = game.Workspace.Speakers.Model.Part

local p = game.Workspace.Speakers:GetChildren()


wait(20)


while true do
	
	local song = songs[math.random(1, #songs)]
	print(song)
	
	if check.Sound.TimePosition == check.Sound.TimeLength then
		
		for i,v in pairs(p) do
			v.Part.Sound.Playing = false
			v.Part.Sound.SoundId = song
			v.Part.Sound.Playing = true
		end
		
	end
		
	wait(1)
end
1 Like

I would recommend instead using the sound.Ended event, you could connect a function to this

sound.Ended:Connect(function(soundID)
	print(soundID) -- this is the soundID that just ended
end)

https://create.roblox.com/docs/reference/engine/classes/Sound#Ended

3 Likes

Got it working, thank you for this!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.