How to detect if a player is NOT in region3

I want to detect if a player is not in region3

I know how to detect if a player is in region3 but I don’t know how to detect if it’s not.
This is what I have so far it only detects when a player is in region3

local region = Region3.new(Vector3.new(-26.454, 234.502, 86.142),Vector3.new(3.749, 258.752, 112.11))

while wait() do
	local partInRegion = workspace:FindPartsInRegion3(region, part, math.huge)
    for _, part in pairs(partInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			print("You are in region3")
		end
	end
end

The easy way (and probably not as efficient as others) is by adding a not operator in the if statement:

local region = Region3.new(Vector3.new(-26.454, 234.502, 86.142),Vector3.new(3.749, 258.752, 112.11))

while wait() do
	local partInRegion = workspace:FindPartsInRegion3(region, part, math.huge)
    for _, part in pairs(partInRegion) do
		if not part.Parent:FindFirstChild("Humanoid") ~= nil then --Added the not here
			print("You are not in region3")
		end
	end
end

if you put not in the if statement in wont work because it will just go trough each part and if the part doesn’t have a humanoid in it it will print that out. So basically it will just keep on printing out “You are not in region3”

I do not recommend doing while wait() do. I would recommend using RunService.Stepped:Wait() instead. Here is the article from Developer Hub if you wish to use Stepped.

I fixed it by making a table and if the player is in region3 I put the player in the table and I reset the table every loop and then it checks if the player is not in the table and if not then he is not in region3

local region = Region3.new(Vector3.new(-26.454, 234.502, 86.142),Vector3.new(3.749, 258.752, 112.11))
while wait() do
	local partInRegion = workspace:FindPartsInRegion3(region, nill, math.huge)
	local playersInRegion3 = {}
	for _, part in pairs(partInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			local plr = game.Players:FindFirstChild(part.Parent.Name)
			workspace.CurrentCamera.CameraType = Enum.CameraType.Watch
			table.insert(playersInRegion3,plr)--putting the player in the table
			print(plr.Name.." is in region3")
		end
	end
	if table.find(playersInRegion3,game.Players.LocalPlayer) == nil then --checking if player is not in the table
		print(game.Players.LocalPlayer.Name.." is not in region3")
	end
	playersInRegion3 = {} --resetting the table after every loop
end
2 Likes