Intro Song for Group

Hello there!

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)

That’s because you’re using string numbers at the top and regular numbers at the bottom. Regular numbers are also used to find a value at a given key.

array[1] = first value in the array
array["1"] = referencing key named "1"

So what do I add to the script?

Sorry I am not really knowledgable in scripting :sweat_smile:

You can use tostring() to convert the numbers into strings.

And where do I add this line of code?

Like for example, line 47?

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"][tostring(player:GetRankInGroup(14253378))] then
		playSong(songs["14253378"][tostring(player:GetRankInGroup(14253378))])
	elseif songs["14243751"][tostring(player:GetRankInGroup(14243751))] then
		playSong(songs["14243751"][tostring(player:GetRankInGroup(14243751))])
	elseif songs["14252305"][tostring(player:GetRankInGroup(14252305))] then
		playSong(songs["14252305"][tostring(player:GetRankInGroup(14252305))])
	elseif songs["14252276"][tostring(player:GetRankInGroup(14252276))] then
		playSong(songs["14252276"][tostring(player:GetRankInGroup(14252276))])
	elseif songs["14401043"][tostring(player:GetRankInGroup(14401043))] then
		playSong(songs["14401043"][tostring(player:GetRankInGroup(14401043))])
	end
end)

Although, I’d go about this in a little bit of a different way to avoid having to write everything out like this.

Hmmmm it seems that it doesn’t work. Any more ways to fix this?

Are there any errors or is it just not working?

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.

So I put the sound in the serverscriptstorage?

No, the sound is good where it’s at. The sound wouldn’t be heard from there.

Why‘s it using .loaded anyways? Doesn’t seem to change a thing using or not using it.