im trying to make a serverside music player that chooses random songs and doesnt repeat the same one.
this is my script and it doesnt work because sound.ended doesnt work.
local songlist = {
3626217233,
6285393445,
4756558939,
1167891823,
830780384,
14459505053,
889213758
}
local count = 0
for i,v in pairs(songlist) do
count = count + 1
end
local song_playing = false
local sound
local Song_Picker = math.random(1,count)
local Previous_Song = Song_Picker
local debounce = false
while wait() do
if not song_playing then
song_playing = true
Song_Picker = math.random(1,count)
while Song_Picker == Previous_Song do
Song_Picker = math.random(1,count)
end
end
if Song_Picker ~= Previous_Song and not debounce then
debounce = true
sound = Instance.new("Sound",game.SoundService)
sound.SoundId = ("rbxassetid://" .. tostring(songlist[Song_Picker]))
sound.Looped = false
sound:Play()
end
end
sound.Ended:Wait()
print("Asdf")
song_playing = false
debounce = false
sound:Destroy()
Previous_Song = Song_Picker
i cant use timelength and timeposition because its not replicated.
You are in an infinite loop. Once the code inside of the loop is done, the loop will simply repeat. I suggest placing the sound.Ended:Wait() statement inside the loop (at the end).
Edit: I saw the rest of the code in your script, and I think that you should probably also be added to the loop.
local songlist = {
3626217233,
6285393445,
4756558939,
1167891823,
830780384,
14459505053,
889213758
}
local count = 0
for i,v in pairs(songlist) do
count = count + 1
end
local song_playing = false
local sound
local Song_Picker = math.random(1,count)
local Previous_Song = Song_Picker
local debounce = false
while wait() do
if not song_playing then
song_playing = true
Song_Picker = math.random(1,count)
while Song_Picker == Previous_Song do
Song_Picker = math.random(1,count)
end
end
if Song_Picker ~= Previous_Song and not debounce then
debounce = true
sound = Instance.new("Sound",game.SoundService)
sound.SoundId = ("rbxassetid://" .. tostring(songlist[Song_Picker]))
sound.Looped = false
sound:Play()
sound.Ended:Wait()
print("Asdf")
song_playing = false
debounce = false
sound:Destroy()
Previous_Song = Song_Picker
end
end
yeah i realized the sound.ended:Wait() was after the loop but still it doesnt work for some reason. it just stops and it doesnt play the next song after putting it into the loop
I just realized, i was changing the timeposition in workspace but timeposition isnt replicated so its changing it client side, i have to wait until the song actually finishes to see if it plays the song. I’m not sure though if this is true
yeah it works. this was my most embarrassing post tbh.