Region3 - Contradicting values?

I’m currently working on a Region3 location detector - So if a Player is within a set Region3 - They’re registered as being in that “location”.

My issue is that as each Region3 is generated using the in pairs displayed below, it contradicts itself when stating the Player is not within a viable Region3; how could I counteract this?

	if v:FindFirstAncestor(Player.Name) then
		script["Region"].Value = p.Name
	else
		script["Region"].Value = "N/A"
	end


local HUD = script.Parent.Parent
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait(1)
Character.Archivable = true

repeat wait() until Character ~= nil

for _,i in pairs (game:GetService("Workspace"):GetDescendants()) do
	if i:IsA("Configuration") and i.Name == "Region3" then
		for _,p in pairs (i:GetChildren()) do
			
			local Region = Region3.new(p["Point1"].Position, p["Point2"].Position)
			spawn(function()
				while true do wait(1)
					local _a = game:GetService("Workspace"):FindPartsInRegion3(Region, game:GetService("Workspace")["Terrain"])
					for _,v in pairs (_a) do
						if v:FindFirstAncestor(Player.Name) then
							script["Region"].Value = p.Name
						else
							script["Region"].Value = "N/A"
						end
					end
				end
			end)
			
		end
	end
end

EDIT
Even attempted to filter through the table afterwards to see whether to Player is within any region. No hope.

Where is this script? In the player?

If so, you should break out of the loop after you set the region.

if v:FindFirstAncestor(Player.Name) then
    script["Region"].Value = p.Name
    break --add this
else
--...
1 Like

Oh, also, I don’t think this code will ever run, right? Where are you updating the Character value so that the first line will stop waiting? That line should be something like this:

local Character = game.Players.LocalPlayer.Character
if not Character then
    Character = game.Players.LocalPlayer.CharacterAdded:Wait()
end

Or just don’t have the character check at all. You don’t use it in the loop.

Also in the spirit of code review

  1. You should try to use better variable names. i, p, _a, v are all sort of difficult to understand. configs, positions, parts, part, respectively would be better.

  2. spawn is generally discouraged, especially as you’re using it here (in a loop of indefinite size). I would recommend using a single loop (in a coroutine if needed) for all the regions in order, since there’s no need to run this in parallel.

  3. This seems like its a localscript. Also in the spirit of reducing thread count, I would just have a single loop on the server which assigns regions to players. It helps defend against exploiters as well, since you’re taking control away from the client.

  4. workspace:GetDescendants() is going to loop through every item in workspace. That can get pretty expensive. Consider putting your Configurations in a known location instead, so you don’t have to loop through workspace to find them.

1 Like