I have a script which basically runs a song when someone with that specific rank joins the game, but it isn’t working.
I made sure to give the permissions, I checked other work from the Toolbox but it isn’t what I need. I have tried looking around the the Devforum but I couldn’t find anything related to this topic.
local songs = {
["14253378"] = {
["14"] = 9616160367,
["13"] = 9616159629,
["9"] = 9616158066,
["5"] = 9616165516,
["4"] = 9616164972,
},
["14243751"] = {
["16"] = 9616166692
},
["14252305"] = {
["11"] = 9616208762,
["10"] = 9616161750,
["9"] = 9616232668
},
["14252276"] = {
["11"] = 9616164122;
},
["14401043"] = {
["7"] = 9616162852;
["6"] = 9616162852;
["5"] = 9616162852;
["4"] = 9616162852;
["3"] = 9616162852;
["2"] = 9616162852;
["1"] = 9616162852;
},
}
function checkIfLoaded(song)
return song.IsLoaded
end
function playSong(id)
local song = Instance.new("Sound")
song.SoundId = "rbxassetid://"..id
song.Parent = game:GetService("SoundService")
song.Volume = 10
song.Loaded:Connect(function()
song:Play()
end)
song.Ended:Connect(function()
song:Destroy()
end)
end
game:GetService("Players").PlayerAdded:Connect(function(player)
if songs[14253378][player:GetRankInGroup(14253378)] then
playSong(songs[14253378][player:GetRankInGroup(14253378)])
elseif songs[14243751][player:GetRankInGroup(14243751)] then
playSong(songs[14243751][player:GetRankInGroup(14243751)])
elseif songs[14252305][player:GetRankInGroup(14252305)] then
playSong(songs[14252305][player:GetRankInGroup(14252305)])
elseif songs[14252276][player:GetRankInGroup(14252276)] then
playSong(songs[14252276][player:GetRankInGroup(14252276)])
elseif songs[14401043][player:GetRankInGroup(14401043)] then
playSong(songs[14401043][player:GetRankInGroup(14401043)])
end
end)
This is mostly correct, however you’re trying to use SoundService. This, I believe, is not how SoundService works and you should be parenting it to something like Lighting. Workspace would then be affected by distance so that doesn’t work as well.
SoundService is actually the preferred place you’re supposed to use, just how ServerStorage/ReplicatedStorage replaced storing things in the Lighting. But that’s entirely optional.
I think the actual issue is that it’s using song.Loaded, which may have loaded before the event was even connected. I would just play or yield until loaded and then play.