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

How to make area specific music?

I've looked heaps and can never find out how to do this, does anyone know how to make music play LOCALLY when you touch a part and to stop any other music playing? e.g. When you enter a doorway, it plays one song, and when you exit it stops the current song and plays a different one.

2 answers

✓ Accepted answer

Just copy the song you want to play into the player then play it. That would make it local. I'll provide an example so I don't look lazy. The below will play a sound locally when a part is touched,

--Assuming the script is inside the part,
local Sound = game.Workspace:WaitForChild("Sound")--Define your sound
Script.Parent.Touched:connect(function(part)
    if not part then return end
    if part.Parent:FindFirstChild("Humanoid") then
        if game.Players:FindFirstChild(part.Parent.Name) then--checks if it's a player
            local plr = game.Players:GetPlayerFromCharacter(part.Parent)
            local SoundCopy = Sound:Clone()
            SoundCopy.Parent = plr--set sound's parent as player
            SoundCopy:Play()--plays sound locally
        end
    end
end)

Now the main problem with this is it will repeatedly replay the song because you repeatedly retouch the brick. An easy way of fixing this is to add a Debounce. A debounce is simply a variable that stops code from running more than once in a short period of time. Here is a link to how and what a debounce is. Here's how you would use a debounce in code,

--Assuming the script is inside the part,
local Sound = game.Workspace:WaitForChild("Sound")--Define your sound
local Debounce = false -- add variable "Debounce"
Script.Parent.Touched:connect(function(part)
    if not part then return end
    if Debounce then return end
    Debounce = true
    if part.Parent:FindFirstChild("Humanoid") then
        if game.Players:FindFirstChild(part.Parent.Name) then--checks if it's a player
            local plr = game.Players:GetPlayerFromCharacter(part.Parent)
            local SoundCopy = Sound:Clone()
            SoundCopy.Parent = plr--set sound's parent as player
            SoundCopy:Play()--plays sound locally
        end
    end
    wait(5)--amount of time until brick can be used again.
    Debounce = false
end)

That's would be how you add a debounce. No to stop music all you have to do is Define you song and do something like

Song:Stop() To stop the Song/Sound.

Hope that helped! Good luck!

7 comments

0

That's NEARLY what I wanted, there are 2 major problems, 1: It constantly plays it while you are touching the brick, and 2: It doesn't stop other music playing

0

Awesome, I'll update my answer

0

Make sure to click the Accept Answer button under my name if I helped! Good luck friend (:

0

Thanks! Having trouble with stopping sound, but I'll figure it out :P

0

<3

0

UPDATE: Figured it out! I'm gonna post it below, just in case anyone wants it, but this will stay the answer, as it is the one that solved what I asked for!

0

Aight.

I edited wfvj014's code to stop other sounds, its probably not very good, but I'mma post it anyway!

NOTE: This is not the answer, nor do I mean it to be, I'm just posting it to show others who may need the same thing how I did it.

--Assuming the script is inside the part,
local Sound = game.Workspace:WaitForChild("Sound")--Define your sound
local Debounce = false -- add variable "Debounce"
script.Parent.Touched:connect(function(part)
    if not part then return end
    if Debounce then return end
    Debounce = true
    if part.Parent:FindFirstChild("Humanoid") then
        if game.Players:FindFirstChild(part.Parent.Name) then--checks if it's a player
            local plr = game.Players:GetPlayerFromCharacter(part.Parent)
            local SoundCopy = Sound:Clone()
            local found = plr:FindFirstChild("Music", true)
            if not found then 
            local Newpart = Instance.new("Part", plr)
            Newpart.Name = "Music"
            Newpart.Transparency = 1
            Newpart.CanCollide = false
            end
            plr.Music:ClearAllChildren()
            SoundCopy.Parent = plr.Music--set sound's parent as player
            SoundCopy:Play()--plays sound locally
        end
    end
    wait(1)--amount of time until brick can be used again.
    Debounce = false
end)

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