Checking Region3 Causes Lag

So I’m using Region3 to play different sounds in different areas of the map.

To do this, I made a folder with the region parts, and just checked them in a while loop every 3 seconds for players. If they aren’t in any of the regions, then they are in the sea if your confused on that part of the script.

However a noticeable lag spike occurs every time it checks. Right now, there are only 2 region parts in the folder. Also, I removed some things and replaced them with comments.

The Script (Local Script):

local regionChecks = {
	["Spawn"] = false,
	["Blue Cove"] = false,
}

local function checkRegions()
	for island, value in pairs(regionChecks) do
		if value == true then
			return true
		end
	end
	return false
end

local function updateLabel(name)
	local guiFunction = coroutine.create(function()
		mapRegionGui.Enabled = true
		mapLabel.Text = name
		--Tweening Frame Here
		wait(.9)
        --Tweening Frame Here
		wait(1)
		--Tweening Frame Here
		wait(1)
		mapRegionGui.Enabled = false
	end)
	coroutine.resume(guiFunction)
end

while wait(3) do
	for i, regionPart in pairs(regionFolder:GetChildren()) do--Looping through each region part
		local mapName = regionPart.Name
		local foundPlayer = false
		
		local min = regionPart.Position - (regionPart.Size / 2)
		local max = regionPart.Position + (regionPart.Size / 2)
		local region = Region3.new(min, max)
		
		local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, character:GetDescendants(), math.huge)
		
		for i, part in pairs(partsInRegion) do
			if part:FindFirstAncestor(character.Name) then
				foundPlayer = true
				break
			else
				foundPlayer = false
				break
			end
		end
		
		if foundPlayer and currentLocation.Value ~= mapName then
			
			--Fading the sound with tween service in a courotine
			
			--Cloning a sound and playing it
			
			updateLabel(currentLocation.Value)
		elseif not foundPlayer and currentLocation.Value ~= "The Deep Sea" then
			
			--Fading the sound with tween service in a couroutine
			
			if checkRegions() == false then
				currentLocation.Value = "The Deep Sea"
				
				--Fading the sound with tween service in a couroutine
				
				--Cloning a sound and playing it
				
				updateLabel(currentLocation.Value)
			end
		end
	end
end

Could be the loop, if there’s a ton of regions inside the folder then that could be causing the lag spike.

This is being done on the client so the host’s PC will have to be doing the math, which can and will hammer the CPU pretty hard, lowering the FPS. Which, unfortunately, cannot be resolved as Roblox will have to fix it.
Or, you could do this on the Server so it wouldn’t lag the client (playing Solo on Studio will lag because the Server’s job is also being done on your PC, on a normal Roblox game instance that isn’t the case).

Hmm there’s only 2 region parts inside the folder. Is there really no way to fix this? And if I did this on the server, would it lag the whole server?

Doing it on the server will make it Roblox’s job to do the calculations so no it wouldn’t lag the whole server.

I recommend doing it from the server then. You can use RemoteEvents and FireClient to a specific player to tell their client to play a different sound when they enter a new region. Shouldn’t be very difficult.

1 Like

Ok, I’ll try that and see if it works

So I did most of it on the server, besides changing the player gui, adding and removing sounds, etc. However, I still get a small lag spike every time it checks. Could it be something in the script?
Should I create one script for each region??

Did you test it with Studio?

If so, things done on the Server also impact your CPU.
Try playing the game by the Roblox website and see if that helps.

1 Like

Yep, looks like uploading and playing it fixed it, unless I’m just not seeing it. Thank you!

1 Like

No problem.

Server lag only is experienced when you Play Solo on Roblox Studio, just keep that in mind for any future projects you’ll be creating.

1 Like