Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Ended event doesn't work correctly?

Asked by 6 years ago
--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?

0
Wrap your while true do from L48 in a spawn(function() end) RubenKan 3615 — 6y

2 answers

Log in to vote
0
Answered by
Nonaz_jr 439 Moderation Voter
6 years ago

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.

0
He could also wrap the loop. optiplex123 21 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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)

Answer this question