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

How to make different sound environments?

In my game, there are three areas, and I'd like to make it so when you're in one area, certain music plays, and when you're in another area, different music plays. How do I do this?

1 answer

local areas = {
	[1]= {
		Name = "Town",
		Center = Vector3.new(0,0,0),
		Music = "13333337"
	},
	[2] = {
		Name = "Toilet",
		Center = Vector3.new(50,0,0),
		Music = "13333337"
	},
	[3] = {
		Name = "Wilderness",
		Center = Vector3.new(0,0,50),
		Music = "13333337"
	}
}

local me = game.Players.LocalPlayer
local sound = Instance.new("Sound")
local currentArea = nil

local function getClosestArea()

	local closestArea = 1
	local minimumDistance = me:DistanceFromCharacter(area[1])
	

	for i,area in pairs (areas) do
		local distance = me:DistanceFromCharacter(area.Center)
		if distance < minimumDistance then
			minimumDistance = distance
			closestArea = i
		end
	end

	return closestArea
end

while wait(1) do
	local closestArea = getClosestsArea()

	areaData = areas[closestArea]

	if currentArea ~= areaData.Name then
		currentArea = areaData.Name
		sound:Stop()
		sound.SoundId = "rbxassetid://"..areaData.Music
		sound:Play()
	end
end

1 comment

0

Uhhh... Can you explain this? Where do I put the script? It is a local script? What does everything do in this script?

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