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

How do I make music specific to location and so only the player in that location hears it?

Asked by 9 years ago

ok here's what I tried : I tried using a local script that connects to a part and when it is touched, it changes the music. The problem with this is that it changes the music for everyone, not just the person that touches it.

1 answer

Log in to vote
0
Answered by
war8989 35
9 years ago

Try placing the sound into their head. Put the following script in a regular script, and read the 2 comments at the top of the script.

sounds = {} -- Don't touch this, it'll generate itself.
-- Create a new model in Lighting called 'Sounds' and put all your sounds in there.

function generateSoundsTbl()
    for _, child in pairs(game.Lighting.Sounds) do
        table.insert(sounds, child.Name)
    end
end

function getNextSound(prevSound)
    local nextSound
    for key, value in pairs(sounds) do
        if(value == prevSound) then
            if(key < #sounds) then
                nextSound = sounds[key + 1]
            else
                nextSound = sounds[1]
            end
        end
    end
    return nextSound
end

function removeSounds(parent) 
    local oldSound
    for _, child in pairs(parent:GetChildren()) do
        if(child:IsA("Sound")) then
            oldSound = child.Name
            child:Destroy()
        end
    end
    return oldSound
end

debounce = false
script.Parent.Touched:connect(function(hit)
    humanoid = hit.Parent:findFirstChild("Humanoid")
    if(debounce == false) then
        debounce = true
        if(humanoid ~= nil) then
            oldSound = removeSounds(hit.Parent.Head)
            nextSound = getNextSound(oldSound)
            sound = game.Lighting.Sounds:findFirstChild(nextSound)
            if(sound ~= nil) then
                sound:clone().Parent = hit.Parent.Head
            end
        end
        wait(0.2)
        debounce = false
    end
end)
generateSoundsTbl()
Ad

Answer this question