Area music not working properly

Goal - When I enter an “area” (partproperties, no collision), music plays

There are multiple part or “Area” in

workspace.StarPlace

, these areas are named “Ice1”
Music located in

workspace.StarPlace.AreaSound

Now when I enter one of the area, the music should play as expected.
When I left the area and enters another area with the same name within 2 seconds, the music should continue playing

However, if I didn’t enter any area within that 2 seconds, the music fades out (this is the part that the script doesn’t work)

The code below was written partly by AI since the first code wasn’t working well.
localscript located in StarterPlayerScript

-- Assuming the Ice1 and AreaSound are already defined in workspace.StarPlace
local Ice1Parts = workspace.StarPlace:GetChildren()
local AreaSound = workspace.StarPlace.AreaSound

-- Get the local player and character
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

-- Debounce variable
local debounce = false

-- Function to fade out the sound
local function fadeOutSound()
	if AreaSound.IsPlaying then
		while AreaSound.Volume > 0 do
			AreaSound.Volume = AreaSound.Volume - 0.05 -- Decrease this value to slow down the fade out
			wait(0.1)
		end
		AreaSound:Stop()
	end
end

-- Function to check if the player is in an Ice1 area
local function isInIce1Area()
	for _, part in ipairs(Ice1Parts) do
		if part.Name == "Ice1" and (Character.HumanoidRootPart.Position - part.Position).Magnitude <= part.Size.Magnitude then
			return true
		end
	end
	return false
end

-- Connect the Touched event to the play function
for _, part in ipairs(Ice1Parts) do
	if part.Name == "Ice1" then
		part.Touched:Connect(function(hit)
			if hit.Parent == Character and not debounce and not AreaSound.IsPlaying then
				AreaSound.Volume = 1
				AreaSound.Looped = true
				AreaSound:Play()
			end
		end)
	end
end

-- Use RunService to continuously check if the player is in an Ice1 area
game:GetService("RunService").Heartbeat:Connect(function()
	if isInIce1Area() then
		debounce = false
	else
		if not debounce then
			debounce = true
			wait(2) -- Wait for 2 seconds to see if the player enters another "Ice1" part
			if not isInIce1Area() then
				fadeOutSound() -- If the player is not in an "Ice1" area, fade out the sound
			end
		end
	end
end)

image
image


^ Ice1 parts, some of them are 2048 long

1 Like

if you guys have some other kind of script that should work it’ll be very lovely! I’ve been trying to fix the script by myself for hours already and this would be the last straw.

I should point that the gap between the Ice1 are seamless since they’re all 0 studs apart.


Hi there,

It seems that you can try using ZonePlus, a module dedicated to detecting zones in places.

Take your time in learning it!

1 Like