Music regions laggy

I made a music region script recently, that detects if you are in a Region3, and finds the correct music. If you arent in any Region3, then it plays the main music.
LocalScript:

local soundRegions = workspace:WaitForChild("SoundRegions")
local music = "Main"

while wait(.1) do
	local found = false
	for i, v in pairs(soundRegions:GetChildren()) do
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local parts = workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
		for _, part in pairs(parts) do
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				music = v.Name
				found = true
				break
			end
		end
	end
	
	if found == false then
		music = "Main"
	end
	
	for i,v in pairs(game.SoundService.Music:GetChildren()) do
		if v.Name ~= music then
			if v.IsPlaying == true then
				v:Stop()
			end
		else
			if v.IsPlaying == false then
				v:Play()
			end
		end
	end
end

Now this script works, but it gives the game a lot of lag spikes all the time.
I have tried to fix this multiple time but failed, so help would be appreciated.

Have you tried to do this by checking the distance of the HumanoidRootPart from the center of the region? Try this edited version of your code.

local Players = game:GetService("Players")

local soundRegions = workspace:WaitForChild("SoundRegions")
local music = "Main"

local plr = Players.LocalPlayer

while wait(.1) do
	local found = false
	
	local hrp = plr.Character:WaitForChild("HumanoidRootPart", 3)
	if hrp then
		local hrpPos = hrp.Position
		local x, y, z = hrpPos.X, hrpPos.Y, hrpPos.Z
		for i, v in pairs(soundRegions:GetChildren()) do
			local regionPos, halfSize = v.Position, v.Size/2
			if math.abs(x-regionPos.X) < halfSize.X and math.abs(y-regionPos.Y) < halfSize.Y and math.abs(z-regionPos.Z) < halfSize.Z then
				music = v.Name
				found = true
				break
			end
		end
		
		if found == false then
			music = "Main"
		end
		
		for i,v in pairs(game.SoundService.Music:GetChildren()) do
			if v.Name ~= music then
				if v.IsPlaying == true then
					v:Stop()
				end
			else
				if v.IsPlaying == false then
					v:Play()
				end
			end
		end
	end
end
1 Like

Seems to have done the trick.
Thanks!

You don’t need Region3s for this as you already have the size and position of the volume in question.

I’m on a phone so dont expect it to be 100% typo free.

local localplayer = game:GetService("Players").LocalPlayer
local lastmusic = --default music sound

game:GetService("RunService").Heartbeat:Connect(function()
    local playerpos = localplayer.Character and localplayer.Character.HumanoidRootPart.Position
    local currentmusic = --default music sound
    if playerpos then
        for _,soundregion in ipairs(soundRegions:GetChildren()) do
            local pos, size = soundregion.Position, soundregion.Size
            local c0,c1 = pos-size/2, pos+size/2
            if playerpos>c0 and playerpos<c1 then
                currentmusic = --whatever sound this region plays
                break
            end
        end
    end
    if lastmusic~=currentmusic then
        lastmusic:Stop()
        currentmusic:Play()
        lastmusic= curremusic
    end
end)