--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'm trying to use the Ended event. For some reason, when the song actually ends, it doesn't print anything and nothing happens. Why is this? Is the ended event broken? Am I using it wrong?
EDIT: Someone gave me answer but the event doesn't work. I was having a dumb moment using the event but when I use it properly, it still doesn't work? I have a play button for the music to actually play. That's in another script. Can that be a problem?
Your using events wrong. You don't use an if statement you need to connect a function to the event for it to work.
Example:
Song.Ended:Connect(function(parameters) -- Connects the event to a function print("Song has ended") -- What happens when the event has been called end) -- Ends the function. Notice the parenthesis at the end "end)".
You could also write the code like this:
function soundEnded(parameters) -- Creates a function print("Song has ended") -- What happens when the function has been called end -- Ends the function Song.Ended:Connect(soundEnded) -- Calls the function
-- You could call the function like this
Song.Ended:Connect(function() soundEnded() end)
You can put code inside of the function for what you want to happen. This goes for all events. Hope this helps
You can check this link out for more details:
http://wiki.roblox.com/index.php?title=RBXScriptSignal