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

Changing player's music depending on level?

I have a leaderstats, depending on the number it is I want a song to play,

game.Players.PlayerAdded:connect(function(player)
	local checkpoint = player:WaitForChild('leaderstats').Checkpoint
	checkpoint.Changed:connect(function()
		if checkpoint.Value == 2 then		
			player.PlayerGui.StageMusic.SoundId = "rbxassetid://BLAHBLAHBLAH"
			player.PlayerGui.StageMusic:Play()			
		end
	end)
end)


First off, it doesn't check if the checkpoint.Value is actually 2, it does it all the time regardless and second is that I keep getting an error

1 answer

✓ Accepted answer

I have created the script below with comments explaining what is happening.

--// Create a table for use below, instead of having to manually make lots of if statements
local soundIds = { 
	00000, --// Automatically indexed as 1
	[2] = 00000, --// Manually indexed as 2
	--// etc.
}

--// Why use a while loop when you can use events?
game.Players.PlayerAdded:connect(function(p)
		--// Don't forget to WaitForChild on the actual value..
		local checkpoint = v:WaitForChild('leaderstats'):WaitForChild('Checkpoint')
		local music = v.PlayerGui:WaitForChild("StageMusic")
	
		local function changeMusic() --// Function to set music
			music:Stop()
			music.SoundId = "rbxassetid://" ..soundIds[val.Value] --// Indexes the sound ID of the current stage
			music:Play()
		end

		p.CharacterAdded:connect(function()
			--// Re-indexes the music object, since PlayerGui resets on spawn by default
			music = v.PlayerGui:WaitForChild("StageMusic")
			changeMusic()
		end)

		checkpoint.Changed:connect(function(val)
			if val == "Value" then --// If the property that changed is the 'Value'
				changeMusic()
			end
		end)
end)

Hope I helped!

~TDP

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