Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make different sound environments?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago
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
0
Uhhh... Can you explain this? Where do I put the script? It is a local script? What does everything do in this script? Ender_Derp -3 — 6y
Ad

Answer this question