--Variables Song = script.Parent.Song SongName = script.Parent.BottomSection.SongName Album = script.parent.BottomSection.AlbumCover local ContentProvider = game:GetService("ContentProvider") times = script.Parent.BottomSection.Length Music = { { ['song'] = 'Bryson Tiller - Been That Way', ['artist'] = 'Bryson Tille', ['itemId'] = 'rbxassetid://313095574', ['length'] = 50 }, { ['song'] = 'Drake - God\'s Plan', ['artist'] = 'Drake', ['itemId'] = 'rbxassetid://1375384985', ['length'] = 50 }, { ['song'] = 'Bryson Tiller - You Got It', ['artist'] = 'Brsyon Tiller', ['itemId'] = 'rbxassetid://977685900', ['length'] = 50 } } local getRandomSongId = function() return Music[math.random(1,#Music)]["itemId"] end Song.SoundId = getRandomSongId() for _,v in pairs(Music)do if v.itemId == Song.SoundId then print("Found the correct array") SongName.Text = v.song end end --time length while true do times.Text = Song.TimePosition wait(0.00000009) end Song.Ended:connect(function() print("hi") --This part right here. When the song ends, nothing actually prints. end)
So I have this music player I'm working on, but when I test the ended event, it doesn't actually work. Is it because you have to press play and you can press pause? Or is the event just not working? Is there a solution? What do I do?
Haha well the reason is,
on line 48 you start a while true loop, which goes on forever. The bit of code that connects your function to the event is never reached or executed.
Move it a bit up (aka the while loop to the bottom) and it will work fine.
try this code instead , what i did is add spawn(function() and end) at the end of whilte true do while loops stop the rest of the script if not spawn(function()
--Variables Song = script.Parent.Song SongName = script.Parent.BottomSection.SongName Album = script.parent.BottomSection.AlbumCover local ContentProvider = game:GetService("ContentProvider") times = script.Parent.BottomSection.Length Music = { { ['song'] = 'Bryson Tiller - Been That Way', ['artist'] = 'Bryson Tille', ['itemId'] = 'rbxassetid://313095574', ['length'] = 50 }, { ['song'] = 'Drake - God\'s Plan', ['artist'] = 'Drake', ['itemId'] = 'rbxassetid://1375384985', ['length'] = 50 }, { ['song'] = 'Bryson Tiller - You Got It', ['artist'] = 'Brsyon Tiller', ['itemId'] = 'rbxassetid://977685900', ['length'] = 50 } } local getRandomSongId = function() return Music[math.random(1,#Music)]["itemId"] end Song.SoundId = getRandomSongId() for _,v in pairs(Music)do if v.itemId == Song.SoundId then print("Found the correct array") SongName.Text = v.song end end --time length spawn(function() while true do times.Text = Song.TimePosition wait(0.00000009) end end) Song.Ended:connect(function() print("hi") --This part right here. When the song ends, nothing actually prints. end)