How to check region3 values of a player

  1. What do you want to achieve? I want to have a script return false if “HitPlayer” is in “region”

  2. What is the issue?


local region = Region3.new(Vector3.new(-17.183, 136.621, 331.28), Vector3.new(131.677, 136.721, 137.972))

function PlayerDamager:CanDamageHumanoid(DamagingPlayer,Humanoid)
	local HitPlayer = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
	if (something) then
		return false
	end 
	
	return true
end

I couldn’t find out how to check what the region of HitPlayer is

  1. What solutions have you tried so far? I checked the DevHub and that actually helped me create the region3 value. I also did some looking on the DevForum but I couldn’t find anything like this. (Note: I don’t know if I have to say this but this script is from a free model sword that I have heavily modified)

You need to look up how to use workspace:FindPartsInRegion3(your region) it returns a table of all parts in the region 3,

This helps but that article is very basic.

local region = Region3.new(Vector3.new(-17.183, 136.621, 331.28), Vector3.new(131.677, 136.721, 137.972))
workspace:FindPartsInRegion3(region)

It prints nothing and because I don’t know what the name of the table is I can’t print it with “print”

Edit: I tried this

local region = Region3.new(Vector3.new(-17.183, 136.621, 331.28), Vector3.new(131.677, 136.721, 137.972))
local a = workspace:FindPartsInRegion3(region)
print (a)

it outputs " [table: 0x35dcc8106ecf418d]" and I don’t know what to do with this

It returns a table of parts in region3, loop through that table in order to get those parts.
Here’s an example in case you need it;

for i, v in pairs(a) do
    print(v.Name)
end

If i recall right the “v” means Value and “i” means index

local region = Region3.new(Vector3.new(-17.183, 136.621, 331.28), Vector3.new(131.677, 136.721, 137.972))
local a = workspace:FindPartsInRegion3(region)
for i, v in pairs(a) do
    print(v.Name)
end

I also don’t know what I should modify in this example, I also tried it as a Local and Server script but still no output.

Yeah, v stands for value and i for index. If there’s not output this means your table is empty meaning there were no objects found in your region3.

uh, no I’m sure I have stuff in that area (such as Models) also I would like to remind myself and others that we are trying to check if a player (“HitPlayer”) is in “region”

edit: I also tried increasing the Y value of the 2nd region

In that case, you’d have to constantly keep checking for objects in that region3 otherwise it’s only gonna run once the script loads in.

Uh?

while true do
local region = Region3.new(Vector3.new(-17.183, 136.621, 331.28), Vector3.new(131.677, 156.721, 137.972))
	local a = workspace:FindPartsInRegion3(region)
		for i, v in pairs(a) do
   			print(v.Name)
		end

end

Like this? I get " [Game script timeout"

Sadly Roblox doesn’t offer a some sort of :IsInRegion() method, which would be very useful. Here is a manually written function that does this for you.

local NormalIds = Enum.NormalId:GetEnumItems()
function insideRegion(cf,size,point)
     for i = 1, #NormalIds do
          local enum = NormalIds[i]
          local norm = Vector3.FromNormalId(enum)
          local worldnorm = cf:vectorToWorldSpace(norm)
          local distance = (norm * size/2).Magnitude
          local planepoint = cf.Position + worldnorm * distance
          if (point - planepoint):Dot(worldnorm) > 0 then
               -- not inside region
               return false
         end
     end
     -- inside region
     return true
end

My apologies for being annoying, I don’t know what those variables mean. The function looks great though!

Well this function is a rabbit hole to explain, you don’t really need to understand it in order to use, just use it like a normal built-in function added by roblox. The first parameter is the CFrame of the Region3 (region.CFrame) and 2nd is the Region3’s size (region.Size) and 3rd is the position of the object that you want to see if is in the region3.

if insideRegion(region.CFrame, region.Size, HitPlayer.Position) then
     return false
end

Another simple thing to do would be this

for i, v in pairs(region:FindPartsInRegion3(region) do
     if v == HitPlayer then
          return false
     end
end
return true

Basically loops through all the parts inside of the region3, and if the player is one of those parts meaning he’s in the region3, return false