How to detect if a player isn't in a region anymore

Is there a way to detect if player is no longer in a region because the else statement in the script below doesn’t work and I tried doing if not Part.Parent:FindFirstChild(“Humanoid”) but that didn’t work either. If you know how, please let me know.

local RegionPart = script.Parent 
local pos1 = RegionPart.Position - (RegionPart.Size/2)
local pos2 = RegionPart.Position + (RegionPart.Size/2)
local Region = Region3.new(pos1,pos2)
local TestValue = script.Parent.Parent.TestValue
local runOnce = 1
local runOnce2 = 1
while wait(1) do
	local partsInRegion = workspace:FindPartsInRegion3(Region, nil, math.huge)
	local playersInRegion3 = {}
	for i, Part in pairs(partsInRegion) do
		if Part.Parent:FindFirstChild("Humanoid") then
			if runOnce == 1 then
				runOnce = runOnce + 1
				TestValue.Value = true
				print("works")
			end
			local plyrs = game.Players:FindFirstChild(Part.Parent.Name)
			table.insert(playersInRegion3,plyrs)
			Part.Parent.Humanoid:TakeDamage(1)
		else
			if runOnce2 == 1 then
				runOnce2 = runOnce2 + 1
				TestValue.Value = false
				print("this works too")
			end
		end
	end
	
end

1 Like

maybe this helps

I think you’d be better off looping through all the players rather than all the parts in the region.

You then need to manually check if the player is in the region by looking at their position and comparing it to your pos1 and pos2 variables, I can give pointers for that as well if you need.

I’m only saying check manually because I don’t think there’s an in build method that’s like Part:CheckIfPartInRegion3(region3)