Music in different Areas

I’ll try my best, I’ve never been a great teacher.

The first thing you’re going to want to do is to add your sounds into SoundService. Name one sound “Sound1” and the other one for Room 2 “Sound2”.

Now add a RemoteEvent into ReplicatedStorage. Title it “SoundEvent”. From here, insert a brick where the first room meets the 2nd room, so when they cross into the 2nd room they touch the brick. Make it’s transparency 1, CanCollide false, and make sure that players can’t accidentally pass by it without touching it.

Insert a script into the brick. You’re going to want to set the code in the script to this:

local players = game:GetService("Players")

script.Parent.Touched:Connect(function(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid then
local char = part.Parent
local target = players:GetPlayerFromCharacter(char)
game.ReplicatedStorage.SoundEvent:FireClient(target)
end
end)

Now the game detects when the player crosses into the 2nd room. Now we need to change the song. Place a localscript in StarterPlayerScripts (under StarterPlayer in explorer) and make this the code:

game.ReplicatedStorage.SoundEvent.OnClientEvent:Connect(function()
game.SoundService.Sound1.Playing = false
game.SoundService.Sound2.Playing = true
end)

Hope this works!

8 Likes

Okay i tried it. When i touched the Invisible wall it played the Music. When i stopped touching it, the Music still played that Song.

Do you have discord ? Maybe we could call and Chat so i can describe it better.

1 Like

Why would it need to stop playing when they stop touching the brick? Are the players able to walk back into room one once they enter room two?

As for discord, yes I do, but I am on my phone right now and don’t have the Discord App on my phone. Perhaps later.

2 Likes


This is the Starting Room. I want that one Music Plays in this room. You see a large Door with a small sign next to it. This is the first Obstacle Course.


This is how it Looks like when you walk through the door. This room should not Play the Music from the beginning and should stay quiet. At the end of this room is a small red Portal which teleports the Player into the first obstacle Course.


This is the first obstacle Course. There should Play a new Music until the Player hits the teleporter at the end and gets back to the beginning Area.

I hope These Pictures are helping to understand it.

Oki i will try to explain it as detailed as possible. You see 3 Areas. Beginning Room. Teleport Room and the Obstacle Course Room. Beginning Room and Obstacle Room Need to Play their own theme while teleport room stays silent.

2 Likes

So this is your problem, a sound emitter emits sound in a circle radius. You may want to develop your map around this circle. If you don’t want to adjust your map, you can look into having the sounds be muted from the client when you move to a different area

3 Likes

But there is another Problem. If a brick has that Music inside of it. The Music gets louder if you are in the Center of the brick. So if you stand in a Corner then the Music would be a Little bit quiet and if you near the Center it will get louder. I once had that Problem with one of my older games :frowning:

2 Likes

So you don’t want the music to be different volumes in different parts of Ann area?

3 Likes

Idk how to explain that qwq. Add me on Roblox and i will Show you the Problem in that game.

3 Likes

Can’t really join game rn, however I can write some code up later. Just try to explain what you are trying to get as best as possible here. My suggestion currently would to maybe use region3 WorldRoot | Documentation - Roblox Creator Hub

3 Likes

Oof im trying to send a Video but it isnt working. So if i put a large brick on the Ground and stay on the Corner, the Music is quiet. But if i walk to the Center of the Brick the Music gets louder. But i want that the whole brick or room has the same Volume and not getting quieter even tho im still on the brick and just walked a bit away from the Center of it. I dont know. Maybe i have to Change the radius of the Music but then it would take so Long to move the camera in roblox Studio from area 1 to area 2

1 Like

Ok, I understand what you are trying to get, will post some code once I get to my PC to create some

2 Likes

Thank you. I will wait for your answer. ^^

1 Like

Ok, here is the code I created, I have created notes so you can take this and maybe modify it to your needs

First this is how I set everything up
image

I made the zones not fully transparent so you could see them
image

You can download the place here MusicPlaying.rbxl (21.1 KB)
or copy the source code and copy the setup

source code

local whiteList = {} -- Whitelist for zone
if not(game.ReplicatedStorage:FindFirstChild('MusicEvent')) then --Checks if there is an event, otherwise it creates one
	Instance.new("RemoteEvent",game.ReplicatedStorage).Name = 'MusicEvent'
end
local event = game.ReplicatedStorage:FindFirstChild('MusicEvent') --Defining event

game.Players.PlayerAdded:Connect(function(player) --Fires when the player is added
	player.CharacterAdded:Connect(function(char) --Fires when the character is added
		table.insert(whiteList,char) --Adding the character to the white list
	end)	
	player.CharacterRemoving:Connect(function(char) --Fires when the character is benig removed
		table.remove(whiteList,table.find(whiteList,char)) --Removing the character from the white list (if the player dies or leaves)
	end)
end)

while true do --Loop
	
	for i,v in ipairs(game.Workspace.MusicZones:GetChildren()) do -- Gets all the music Zones
		local min = v.Position - v.Size*.5 -- Getting the size of the zone
		local max = v.Position + v.Size*.5 -- Getting the size of the zone
		local parts = workspace:FindPartsInRegion3WithWhiteList(Region3.new(min,max),whiteList,100)
		-- That gets the parts in the zone
		for h,d in ipairs(parts) do -- Going through each part
			
			if d.Name == 'HumanoidRootPart' and game.Players:GetPlayerFromCharacter(d.Parent) and v:FindFirstChild('Music') then
				-- Checking if the name is HRP and the player is in the game and there is a music ID
				event:FireClient(game.Players:GetPlayerFromCharacter(d.Parent),v:FindFirstChild('Music').Value)
				-- Sends the music ID to the player
			end
		end
	end
	
	wait(1) -- Waits one second before doing the loop again
end
local event = game.ReplicatedStorage:WaitForChild('MusicEvent')
-- Defines event

event.OnClientEvent:Connect(function(musicId) --Fires when the event is called
	if script.Parent.Music.SoundId ~= musicId then
		script.Parent.Music.SoundId = musicId
		print('now playing '..musicId)
		script.Parent:Play()
		-- Plays the new sound
	else
		print('still playing '..musicId)
		--Still playing the old sound
	end
end)

Make sure you copy everything over, and to change the music, change the Music Value under the zone part. Good luck on your project!

9 Likes

I just got back from my Dad’s Birthday party, so sorry for the delay @Cepper1232YT. Anyways, as I was saying.

Detecting whether or not they’re in a radius is very counter-productive, because now not only does every room need to be a perfect circle, but a lot of math is involved. Having bricks by each door leading to a new room is your best bet.
A brick going from lobby to “intermission” room that would turn off lobby music, and then another block that goes from intermission room to the actual obby, which activates the obby music. Finally a brick that’s at the end that when you’re teleported back, ends the obby music and plays the lobby music. Also should I add the scripting for this is a lot less of a burden and easier to understand for someone who’s new to scripting like him.

4 Likes

I agree with @kingerman88 it would be best to use Region3 for this instance.

2 Likes

So i opened it and looked at everything. Do i have to add the Music ID into the scripts where it’s saying “musicId” or do i have to Change the Value in the bricks ?

1 Like

You only need to change the value in the brick

2 Likes

So the Value is the Music ID ?

2 Likes

Any idea wether this is across clients or universal? cuz its server side…

Or better, just use the VolumetricAudio property of SoundService and see how that works.