Region3 making performance horrible

Hello devforum,
I’m having some problems using region3. When I try to use it on multiple parts (with a loop) to see if a player is in any of those parts (I have to check it every frame) I get horrible performace problems (from 100 fps to 20). This is the code (it’s client sided).

for _, waterPart_v in pairs(waterParts) do
     for _, v in pairs(workspace:FindPartsInRegion3(getRegion3FromPart(waterPart_v))) do
          if v.Parent == character then
              partsInObject = true
          end
     end
 end

There are 4 gigant parts inside the waterParts table.

Is there any other way of achieving the same result in a different way? Please give me suggestions.
Thanks for reading.

I’m not sure if this improves performance and hopefully somebody can confirm, but there does exist a function called FindPartsInRegion3WithWhitelist. just add the character model to the whitelist.

apart from that, I seriously doubt you need a check every frame. if running something every frame goes poorly, usually I’d suggest 1/15 of a second tops.

edit: only turn down the checks if you continue to experience lag after the first method, otherwise go for it.

2 Likes

I had made something similar except for a vehicle shop and I had a somewhat iffy method of doing it.
You’ll have to make your subtle modifications of course but this is a starting point of how you can achieve your final goal.

local API = {}
local RunService = game:GetService("RunService")
local AllRegions = {}

function IsInAnotherRegion()
	for _, UserInAnotherRegion in pairs(AllRegions) do
		if UserInAnotherRegion == true then
			return true
		end
	end
	
	return false
end

function IsInRegion(Parts)
	for i, LocateHumanoid in pairs(Parts) do
		if LocateHumanoid:IsA("Humanoid") or LocateHumanoid.Parent.Name == game.Players.LocalPlayer.Name then
			return true
		end
	end
	
	return false
end

function API:Initialize()
	for i, WaterInstance in pairs(game.Workspace.Interactions.PaintShop:GetChildren()) do
		local Region = Region3.new(WaterInstance.Position - WaterInstance.Size/2, WaterInstance.Position + WaterInstance.Size/2)
		local Player = game:GetService("Players").LocalPlayer
		
		AllRegions[tostring(WaterInstance.Position)] = false
		
		RunService.Heartbeat:Connect(function()	
			local PartsInRegion = workspace:FindPartsInRegion3WithWhiteList(Region, game.Players.LocalPlayer.Character:GetChildren(), math.huge)
			
			if IsInAnotherRegion() == false then
				-- remove any water effects
			end
			
			if IsInRegion(PartsInRegion) == true then
				AllRegions[tostring(WaterInstance.Position)] = true
			else
				AllRegions[tostring(WaterInstance.Position)] = false
			end
		end)
	end
end
return API
1 Like