I'm making a music controller. When the track ends another doesn't start playing. It won't resume/pause either and the GetTimePlayed bindable function just returns the same value. I can't figure out how to fix this. It prints the statement in the pause function but not in the restart function.

Code:

-- Put music ids in here
local music = {
	1354485825,
	1493117981,
	2550851246,
	6259395075,
	4254599057
}

-- Specify the wait time between tracks
local wtime = 5

-- Create the sound object
local sound = Instance.new("Sound", game.SoundService)
sound.Name = "Music"

-- Create the random object
local random = Random.new()

-- Setup the PauseTrack listener
script.Listeners.PauseTrack.Event:Connect(function()
	print("Pausing...")
	sound.Playing = false
end)

-- Setup the ResumeTrack listener
script.Listeners.PauseTrack.Event:Connect(function()
	sound.Playing = true
end)

-- Setup the GetTimePlayed function
script.Functions.GetTimePlayed.OnInvoke = function()
	return sound.TimePosition, sound.TimeLength-sound.TimePosition
end

-- Setup the GetId function
script.Functions.GetId.OnInvoke = function()
	return sound.SoundId
end

-- Define the internal play function
function play()
	-- Choose the music at random
	local musicitem = random:NextInteger(1, #music)
	local musicnumid = music[musicitem]
	local musicid = "rbxassetid://"..tostring(musicnumid)
	-- Setup the sound object
	sound.SoundId = musicid
	-- Start the music
	sound.Playing = true
	-- Tell your scripts the music has started
	script.Broadcasters.TrackBegins:Fire()
end

-- Define the internal restart function
function restart()
	print("Restarting...")
	script.Broadcasters.TrackEnds:Fire()
	play()
end

-- Connect the song ending to the internal restart function
sound.Stopped:Connect(restart)

-- Start the music
play()

2 comments

0

Actually I think the only problem is on line 63 you have code that uses a Stopped event try using Ended event like this sound.Ended:Connect(restart)