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

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

Asked by 3 years ago
Edited by JesseSong 3 years 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 222 Moderation Voter
3 years 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 222 — 3y
0
It doesn't work :( imnotaguest1121 362 — 2y
Ad

Answer this question