Ad
Log in to vote
1

Why Is area music still playing when the player exits the area?

Asked by 7 months ago
Edited by JesseSong 7 months ago

I am making a script where If you touch a block It plays music and when you leave it the music stops kinda like a sound region or area music. But the problem is that it doesn't stop when the player gets out of the area, meaning that the audio does play but doesn't stop when the player exits the area. This Is the script:

local part = script.Parent
local sound = part:FindFirstChild("Sound")

local function music(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
    if humanoid then
        sound:Play()
        if not humanoid then
            sound:Stop()
        end
    end
end


part.Touched:Connect(music)

Reformed by JesseSong (Fixed grammatical errors)

1 answer

Log in to vote
0
Answered by
A_Mp5 221 Moderation Voter
7 months ago

This is due to it mattering if humanoid is found, rather than seeing if the player stopped touching, you would, and it would stop if a model with no humanoid hit it. You would have to make another function that would be touchended.

local part = script.Parent
local sound = part:FindFirstChild("Sound")

local function music(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
    if humanoid then
        sound:Play()
        if not humanoid then
            sound:Stop()
        end
    end
end

local function stopmusic(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
    if humanoid then
        sound:Stop()
    end
end


part.Touched:Connect(music)
part.TouchedEnded:Connect(stopmusic)

This is untested, and i don't know if it's right or wrong.

0
OOPS, line 06, 09, and 10 should be deleted OR IT MIGHT GLITCH! A_Mp5 221 — 7mo
0
It doesn't work :( imnotaguest1121 89 — 6mo
Ad

Answer this question