Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why will this only work once?

This is my music player and it only plays one song but doesnt repeat, im not sure what the issue is


local SoundIds = {
'http://www.roblox.com/asset/?id=243372213',
'http://www.roblox.com/asset/?id=319758951',
'http://www.roblox.com/asset/?id=236896548',
'http://www.roblox.com/asset/?id=181547615',
'http://www.roblox.com/asset/?id=283801001',
'http://www.roblox.com/asset/?id=306412637',
}


while true do --insert and play sound
Music.SoundId = SoundIds[math.random(1, #SoundIds)]
Music.Volume = 1
wait(1)
Music:Play()
Music.Ended:wait() -- Waits for the music to end to play the next music audio
wait(1)
Music:Stop()
end

1 comment

1

You're probably not using a LocalScript meaning on line 16 it keeps on waiting because Scripts can't access that Event on sounds, only Local Scripts can. You're also not defining Music.

1 answer

It's because a song is moderated or that you try to use the script from a LocalScript, Here you have a handy code that can run on the LocalPlayer and on the Server (Workspace, ServerScriptService etc.)

--Config--

local isLocalPlayer=true
local SoundIds = {
'http://www.roblox.com/asset/?id=243372213',
'http://www.roblox.com/asset/?id=319758951',
'http://www.roblox.com/asset/?id=236896548',
'http://www.roblox.com/asset/?id=181547615',
--'http://www.roblox.com/asset/?id=283801001', --This song is moderated which means it cannot be played.
'http://www.roblox.com/asset/?id=306412637',
}

--End of Config--

-- isLocalPlayer will only work in a LocalScript!
-- Place a LocalScript in StarterGui if you want to use isLocalPlayer and make sure the setting it's enabled too :) Oh and don't forget to paste this code into it too :)

local Music

if script:IsA("Script") and not script:IsA("LocalScript") then isLocalPlayer=false end --Some precaution :)

if isLocalPlayer then
Music = Instance.new("Sound",game:GetService("Players").LocalPlayer)
else
Music = Instance.new("Sound",game:GetService("Workspace"))
end
-- I recommend using GetService instead of just game.Players or game.Workspace since if they're renamed, the script will break.

while true do --insert and play sound
Music.SoundId = SoundIds[math.random(1, #SoundIds)]
Music.Volume = 1
wait(1)
Music:Play()
Music.Ended:wait() -- Waits for the music to end to play the next music audio
wait(1)
Music:Stop()
end

This discussion is preserved as it was. New answers, comments, and votes are closed.