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

How do I prioritize sounds?

Asked by 4 years ago
Edited 4 years ago

I have multiple sounds playing at once. Say Billy is over here and John is over there. Once Billy is close enough to hear John's radio, Billy's gui background music turns off so he can hear Johns's music, and once he backs away so he cant hear John's music, his gui background music turns back on.

I am using startergui to house the backrground music and then a simple radio gui that applies a sound to your humanoid root part. So I was like: "Okay I can just see if I am in max range of the sound." So I took the max range of the sound instance and applied it to a magnitude of both humanoids (a for loop for my own player instance with all other players). From there I see if a humanoid is playing a sound, and then if there is one, both player's (the one playing the music and the one listening to it) background musics are turned off until the music is ended

here's my scripts:

local starterGui = script.Parent
local player = game.Players.LocalPlayer
local Run = game:GetService("RunService")
local radius = 20

Run.RenderStepped:Connect(function()
    local char = player.Character
    local root = char:FindFirstChild("HumanoidRootPart")
    local MySounds = 0
    if root:FindFirstChildOfClass("Sound") then
        MySounds = MySounds+1
    end
    for _, players in pairs(game.Players:GetChildren()) do
        if players ~= player then
            if players.Character then
                local chars = players.Character
                local theirRoot = chars:FindFirstChild("HumanoidRootPart")
                local mag = (root.Position-theirRoot.Position).Magnitude
                if mag <= radius then
                    if theirRoot:FindFirstChildOfClass("Sound") then
                        MySounds = MySounds+1
                    end
                end
            end
        end
    end
    if MySounds > 0 then
        for _, sound in pairs(SoundFolder:GetChildren()) do
            if sound.ClassName == "Sound" then
                if sound.IsPlaying == true then
                    sound:Pause()
                elseif sound.IsPlaying == false then
                    sound:Stop()
                end
            end
        end
    elseif MySounds == 0 then
        for _, sound in pairs(SoundFolder:GetChildren()) do
            if sound.ClassName == "Sound" then
                if sound.IsPaused == true then
                    sound:Resume()
                elseif sound.IsPaused == false then
                    sound:Play()
                end
            end
        end
    end
end)

Basically it checks if there is a sound in the vacinity of the local player, and if there is, then the local player's background music is turned off, vice versa with the player playing the music.

I dont have any outcome errors, and when I tested it, It shut off the music, but then when I backed away from the music, it wouldn't turn it back on.

Any help?

Edit: the sound being represented in the script is the background sound. I just want it to check if there is any radio sounds in the other players humanoidrootparts if they are even close enough for the local player to hear

(Hope all my thoughts clear things up)

Answer this question