while true do
print("Choosing new song.")
local sound = script.Parent.Sounds:GetChildren()[math.random(1,#script.Parent.Sounds:GetChildren())]
print("Song chosen is "..sound.Name..".")
print(sound.TimeLength)
sound:Play()
wait(sound.TimeLength)
end
--Hopefully you can help. Thanks!
Alright I edited your script a bit and it seems to work:
while true do
print("Choosing new song.")
local songs = script.Parent.Sounds:GetChildren()
local sound = songs[math.random(1,#songs)]
print("Song chosen is "..sound.Name..".")
sound:Play()
sound.Ended:Wait()
end
Instead of what you’re doing I suggest doing the following:
while true do
print("Choosing new song.")
local sound = script.Parent.Sounds:GetChildren()[math.random(1,#script.Parent.Sounds:GetChildren())]
print("Song chosen is "..sound.Name..".")
sound:Play()
Sound.Ended:Wait(TimeBetweenSongs)
end
Note, the sounds can’t be looped else it’ll probaly glitch.
Thought I suggest that you’d have only 1 sound instance and doing the following:
local TimeBetweenSongs = 1 -- Extra time we'll wait before playing the next song.
local Playlist = { -- Music Ids of the songs we'll play
123456,
1234567,
123457,
654321,
-- If you want to add more sound ids then put the id here and end it with a comma.
}
local Sound = script.Parent.Audio -- Path to the audio instance
local function nextSong(id) -- Every time "nextSong()" is called this will fire.
-- If the parameter "id" is invalid this will break.
Sound.SoundId = id
Sound:Play()
Sound.Ended:Wait(TimeBetweenSongs)
end
while true do
-- Anything inside of this will repeat for ever.
nextSong(Playlist[math.random(1,#Playlist])
end
-- Anything after this will never have the chance to fire.
Took this script from a post I made before, but I edited it.
If you wish to not have the same song playing as before then I suggest doing the following:
local LastSong
local function nextSong(id) -- Every time "nextSong()" is called this will fire.
-- If the parameter "id" is invalid this will break.
Sound.SoundId = id
Sound:Play()
LastSong = id
Sound.Ended:Wait(TimeBetweenSongs)
end
while true do
-- Anything inside of this will repeat for ever.
local CurrentSong
repeat
CurrentSong = Playlist[math.random(1,#Playlist]
wait()
until CurrentSong ~= LastSong
nextSong(Playlist[math.random(1,#Playlist])
end
-- Anything after this will never have the chance to fire.
@ZombieCrunchUK Yes, the problem was the fact that the script was looping even before it could load. Thanks. I just put a wait(5) before the loop. Thanks for helping!