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

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

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

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.

2 comments

0

OOPS, line 06, 09, and 10 should be deleted OR IT MIGHT GLITCH!

0

It doesn't work :(

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