Play music depends on your locations

i want to do what the title says, what i mean by this is:
let’s say we have 2 songs
[“Ruins”] and [“Tem_Shop”]

when player not in shop, then it will play the [“Ruins”] song, and if he enters the shop, it will immidiatly stop [“Ruins”] and play [“Tem_Shop”]

what i tried is creating two parts, one filling the whole shop, and one filling the whole outside shop locations,

and then i tried this script (i used the roblox’s module from the wiki):

-- Roblox services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Require module
local AudioPlayerModule = require(ReplicatedStorage:WaitForChild("AudioPlayer"))
-- Preload music tracks
AudioPlayerModule.preloadAudio({
["Ruins"] = 384418761,
["Tem_Shop"] = 337894733
})

game.Workspace.RuinsSound.Touched:Connect(function()
game.Workspace.GameMusic.Ruins.Volume = 2
AudioPlayerModule.playAudio("Ruins")
game.Workspace.RuinsSound.TouchEnded:Connect(function()
game.Workspace.GameMusic.Ruins:Stop()
end)
end)

game.Workspace.TemSound.Touched:Connect(function()
AudioPlayerModule.playAudio("Tem_Shop")
game.Workspace.TemSound.TouchEnded:Connect(function()
game.Workspace.GameMusic.Tem_Shop:Stop()
end)
end)

so basically everytime you touch a part that in the script, it will play the music, but me wasn’t smart enough to know that Touched event will allways run and it will make the song repeat the first second xd

so i need help, i want the most efficient way to make song plays depends on where you are

4 Likes

If you want to continue using parts to trigger the music, you can add a variable called “playingSound”. Logic similar to this should be what you want for each sound part:


local playingSound = nil

game.Workspace.TemSound.Touched:Connect(function()
    if playingSound and playingSound.Name ~= "Tem_Shop" then
        playingSound:Stop()
        playingSound = nil
    end
    if not playingSound then
        playingSound = TemShopSound(Wherever this is stored)
        playingSound:Play()
        game.Workspace.TemSound.TouchEnded:Connect(function()
            if playingSound and playingSound.Name == "Tem_Shop" then
                playingSound:Stop()
                playingSound = nil
            end
        end)
    end
end)

Edit: Because you are using a module to handling playing the music, you can just store the name of the currently playing music. The logic is the same but you will just make different calls to play the music.

1 Like

it doesn’t repeat the first second of the song , but it is kind of buggy sometimes if you leave the shop and enter back fast it plays the ruins song inside the shop for some reason, or if you jump in the shop it just stops playing sound

Something I’ve been doing lately for this kind of scenario is checking the players location every 0.1 seconds.
Let’s say the shop starts at -10, 0, -10 and ends at 10, 10, 10 (XYZ)

local playerLocation --Player's location
local shopSound --Shop sound
while wait(.1) do
    if playerLocation.X <= 10 and playerLocation.X >= -10 and playerLocation.Y <= 10 and playerLocation.Y >= 0 and playerLocation.Z <= 10 and playerLocation.Z >= -10 then
        if shopSound.IsPlaying == false then
            shopSound:Play()
        end
    else
        if shopSound.IsPlaying == true then
            shopSound:Stop()
        end
    end
end

Might not look as pretty, but it gets the job done

1 Like

This is a good method as it doesn’t rely on the physics engine and can be controlled more predictively. You can also probably get away with a longer wait as well.

1 Like
2 Likes