Region3 Script not Working

For some reason this script doesn’t detect when a player walks into the region3. What I’m trying to do is get 2 points and use those 2 points to create a box where if players walk in the region, it will detect them.

local whitelist = {}
local regions = {}
local model = workspace:WaitForChild("Regions")

for _,part in pairs(model:GetChildren()) do
	local areaName = part.Name
	local point1 = part:FindFirstChild("Point1")
	local point2 = part:FindFirstChild("Point2")
	local reg = Region3.new(point1.Position, point2.Position)
	regions[areaName] = {region = reg}
end


while wait(5) do
	for name,test in pairs(regions) do
		local region = test.region
		local parts = workspace:FindPartsInRegion3WithWhiteList(region, whitelist)
		if parts then
			for _,v in pairs(parts) do
				print(v)
			end
		end
	end	
end

Also I tested whitelist and it works, but I just can’t detect if the player walks inside the region.

2 Likes

Unless you’re setting something into the whitelist, I don’t see anything in there and it just looks like an empty table.

1 Like

I already set the character into the whitelist here’s code;

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		whitelist[Character] = true
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			if Character then
				whitelist[Character] = nil
			end
		end)
	end)
end)
1 Like

You can reference my attempt at Region3, it works as intended.

local Workspace = game:GetService("Workspace")
local Region = Workspace.Region:GetChildren()

function GetPlayerPos(Player, Part)
	wait(0.25)
	for i,v in pairs(Region) do
		local HighPoint = v.CFrame * CFrame.new(Vector3.new(v.Size.X/2, v.Size.Y/2, v.Size.Z/2))
		local LowPoint = v.CFrame * CFrame.new(Vector3.new(-v.Size.X/2, -v.Size.Y/2, -v.Size.Z/2))
		local TopX, TopY, TopZ, BottomX, BottomY, BottomZ
		
		if HighPoint.X > LowPoint.X then
			TopX = HighPoint.X + 0.5
			BottomX = LowPoint.X - 0.5
		else
			TopX = LowPoint.X + 0.5
			BottomX = HighPoint.X - 0.5
		end
		if HighPoint.Y > LowPoint.Y then
			TopY = HighPoint.Y + 0.5
			BottomY = LowPoint.Y - 0.5
		else
			TopY = LowPoint.Y + 0.5
			BottomY = HighPoint.Y - 0.5
		end
		if HighPoint.Z > LowPoint.Z then
			TopZ = HighPoint.Z + 0.5
			BottomZ = LowPoint.Z - 0.5
		else
			TopZ = LowPoint.Z + 0.5
			BottomZ = HighPoint.Z - 0.5
		end
		if Part.Position.X > BottomX and Part.Position.X < TopX then
			if Part.Position.Y > BottomY and Part.Position.Y < TopY then
				if Part.Position.Z > BottomZ and Part.Position.Z < TopZ then
					return true
				else
					return false
				end
			else
				return false
			end
		else
			return false
		end
	end
end

for i,v in pairs(Region) do
	v.Touched:Connect(function(Hit)
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Hit.Parent:FindFirstChild("Humanoid") then
			if GetPlayerPos(Player, Hit.Parent.HumanoidRootPart) then
				--Visible
			end
		end
	end)
	
	v.TouchEnded:Connect(function(Hit)
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Hit.Parent:FindFirstChild("Humanoid") then
			if GetPlayerPos(Player, Hit.Parent.HumanoidRootPart) then
				--Not visible
			end
		end
	end)
end
1 Like

It’s nice, but i’d rather to script this system the way I created in my script where it iterates through each region, and gets the 2 points so if anyone walks into the range it detects.