Region3 Taking up a lot of memory

So I’m creating sound regions using Region3 but there is a big problem. The script runs every 3 seconds, and when it runs the script activity spikes anywhere from 8% to 11%. From my knowledge, I don’t think that’s good at all especially from one small script.

When I first made this, I did it on the client but it caused lag spikes. I then did it on the server and it fixed the lag spikes but the script activity is still high.

Sample of the script(Inside SSS):

while wait(3) do
	print("CHECKING")
	for i, regionPart in pairs(regionFolder:GetChildren()) do--Creating all the regions(2 parts inside the folder so far)
		local mapName = regionPart.Name
		
		local min = regionPart.Position - (regionPart.Size / 2)
		local max = regionPart.Position + (regionPart.Size / 2)
		local region = Region3.new(min, max)
		
		local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)
		
		for i, player in pairs(game.Players:GetPlayers()) do--Testing with only one player
			local character = player.Character
			local found = false
			
			for i, part in pairs(partsInRegion) do
				if part.Parent == character then
					found = true
					break
				else
					found = false
				end
			end
			
			if found then
				foundPlayer(true, character, mapName)
			else
			    foundPlayer(false, character, mapName)
			end
		end
	end
end

I commented out most of the script and what I believe causing the lag is this:

local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)

Region3’s con is that it’s pretty big. I recommend you set the ignore to anything you don’t want.

ALSO

You don’t want to loop this, you should do this on the client and use Distance > (From - To).Magnitude to start the loop!

I once tried it on the client and it caused lag spikes every time it checked. Someone told me handling the math on the server would be better and handling it on the server fixed it. Also how would I used magnitude with Region3?

Using the server would fix performance on the client.

Here is a MOCK UP of how to start the loop on the Client.

local Player = game:GetService("Players").LocalPlayer
local RegionFolder = REGIONFOLDERHERE:GetChildren()

local FINDINREGION3 = function(RegionPart)
    -- Find the Player in the Region
end


local NearRegion = function(HRP)
    for Index = 1, #RegionFolder do
        local Region = RegionFolder[Index]
        local MinDistanceForChar = (HRP.Position - (Region.Poistion - (Region.Size/2))).Magnitude
        local MaxBoundDistance = ((Region.Poistion - (Region.Size/2)) - (Region.Poistion - (Region.Size/2))).Magnitude
        
        if MinDistanceForChar < MaxBoundDistance then
            return FINDINREGION3(Region)
        end
    end
end

local CheckIfNear = function()
    if Player.Character then
        if Player.Character:FindFirstChild("HumanoidRootPart") then
            if NearRegion == true then
                -- DoFunction()
            end
        end
    end
end

game:GetService("RunService").RenderStepped:Connect(CheckIfNear)

It’s a rough mock up, the math of the distance may not be accurate. You could just do

local NearRegion = function(HRP)
    for Index = 1, #RegionFolder do
        local Region = RegionFolder[Index]
        
        if (HRP.Position - Region.Poistion).Magnitude < 20 then
            return FINDINREGION3(Region)
        end
    end
end
1 Like

I ended up doing something similar to your solution. Every 3 seconds, I first find the closest region, check that region, and doing the things from there. It is now fully on the client. But now there is a problem. Last time I did it on the client, it caused lag spikes every time it checked. There is still lag spikes but it’s not as bad. Any idea how to get rid of this?

I ended up ditching region3 and just using magntiude. Thanks for giving the idea of finding the closest region before checking, I don’t think I would of ever think of doing that. This is what i ended up doing:

if HRP then
		local distance = (HRP.Position - regionPart.Position).Magnitude
		if distance <= regionPart.Size.X/2 or distance <= regionPart.Size.Y/2 or distance <= regionPart.Size.Z/2 then
			print("FOUND IN "..regionPart.Name)
			found = true
		elseif distance > regionPart.Size.X/2 and distance > regionPart.Size.Y/2 and distance > regionPart.Size.Z/2 then
			print("NOT FOUND ANYWHERE")
			found = false
		end
	end
5 Likes