Muffle Effect not working as intended

Hey Developers!

I’m trying to fix a script that isn’t working, but I’ve tried many “solutions” and it still doesn’t work!

The problem is when a player is outside of a certain area (aka the TriggerArea) it’s supposed to muffle the sound. When the player is in the TriggerArea, it disables the muffled audio effect, but it isn’t doing any of that and the audio is just muffled.

I’ve tried detecting the humanoid’s X, Y, and Z position every 0.1 seconds (since its an elevator and its moving, but it still isn’t working. (the reason for finding it is to detect if the player is still in the triggerarea)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local sound = game.Workspace.Elevator.Songs.Song1 -- feel free to change this to a song of your own to test it out!
local triggerArea = game.Workspace.Elevator.TriggerArea
local muffleEffect = sound:FindFirstChild("Muffle") 

local function isPointInRegion(point, regionCenter, regionSize)
	local halfSize = regionSize / 2
	local min = regionCenter - halfSize
	local max = regionCenter + halfSize

	return point.X >= min.X and point.X <= max.X
		and point.Y >= min.Y and point.Y <= max.Y
		and point.Z >= min.Z and point.Z <= max.Z
end

while wait() do
	humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		if isPointInRegion(humanoidRootPart.Position, triggerArea.Position, triggerArea.Size) then
			muffleEffect.Enabled = false
		else
			muffleEffect.Enabled = true
		end
	end
end

The muffle effect, trigger area, and sound are all in the right places, but everything I’ve tried just doesn’t seem to work. If you have any solutions or tips on how to fix this problem, please let me know!

1 Like

Open up the Developer Console, and check for any errors there. I can’t appear to replicate this issue on my end ; is it possible for you to post a screenshot of your Explorer, showing your layout and confirming nothing is occurring in the console, and where the LocalScript is located? In addition, consider making your region part visible - your region needs to cover the areas that the HumanoidRootPart is supposed to be in.

I haven’t gotten any errors, but I’ll send a few screenshots to help

TriggerArea and Songs location
image

LocalScript Location
image

Depending on your case, the (most likely cause) of the issue is that your LocalScript is in StarterPlayerScripts. This is because the script is only running one time when the player joins, and the script only gets the player character once - once the player is respawned, the position will never update again. You can see this by printing out the player position in the loop.

There are two easy solutions to this. First, move the script into StarterCharacterScripts, so the script runs every time the character is loaded. Alternatively, get the player’s character again every loop.

Try this, and let me know if the issue remains.

So its disabled when I’m in the elevator, but the audio is still muffled.
image

Could it be because the trigger area is moving? It’s in an elevator after all.