Music Player doesn't work as intended

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? A script that plays music using SoundId’s I referenced.

  2. What is the issue? When a song is a few seconds away from finishing it immediately plays another one but the time position would be the same as the previous. For example, the previous song stops at the time position of 88 seconds, The new one will also play at 88 seconds so instead of playing from the beginning it’s already in the middle of it.

  3. What solutions have you tried so far? I have tried using .Stopped .Ended and Connect, I also tried to use a print but it never showed up in the output.

SoundIds = {"841472657","406892619","5105940701","6468969857","646833689","5612157786","5870275162"}
game.Players.ChildAdded:Connect()
Sound = Instance.new("Sound",game.Workspace)
Sound.Volume = 0
wait(1)
while wait do
	Current = SoundIds[math.random(1,7)]
	Sound.SoundId = "http://www.roblox.com/asset?id="..Current
	Sound:Play()
	Sound.Volume = 0.1
	Sound.Ended:Wait(function()
		Sound.TimePosition = 0
		Sound:Stop()
		print("Sound was stopped, attempting to replay another track...")
	end)
	end
2 Likes

Try waiting sound.TimeLength instead of the Ended connection

1 Like

Can you explain on what I should do? I tried doing so that if the TimePosition is equal to the TimeLength it would do the intended outcome but it didn’t really work out. Really sorry that I didn’t understand.

Maybe instead of waiting for the sound.Ended Connection, you can have task.wait(sound.TimeLength) then continue with the rest of your code

2 Likes

Ohh I understand now. Thank you so much

local Sound = Instance.new("Sound")
Sound.Volume = 0.1
Sound.Parent = workspace

SoundIds = {"841472657","406892619","5105940701","6468969857","646833689","5612157786","5870275162"}

while true do
	Sound.SoundId = "rbxassetid://"..SoundIds[math.random(#SoundIds)]
	if not Sound.IsLoaded then
		Sound.Loaded:Wait()
	end
	Sound:Play()
	Sound.Ended:Wait()
end

Better to use events than to arbitrarily yield the thread.

2 Likes

I’ve tried it and so far it works perfectly!