How would I check if a point is within an area

I want to check for if a Point let’s say (10,10,10) is within an area, like a part

1 Like

Sorry I didn’t say but without parts

That’ll be a hassle if the “area” is not a block

What’s the purpose?

I have something that exists on the client side but only exists as a Point on the server and I don’t want the player to be able to call the RemoteEvent and place the point anywhere they want so I want to check if there is a point within an area of the point being created

Just check if the point is within the position + size/2

That wouldn’t work since the area is not of a square or circle but a rectangle

So get the x, y and z values and half them

Get rectangle position, and check if the point is within half an axis away from all axies. EG. if a point is at 10,10,10, the rectangle is 4,3,2 size and at 9,8,10, you first subtract the position (aka point.Position - part.Position), then absolute value each axis (for the distance not to be negative) which gets you 1, 2, 0 then check if it is less than half an axis away so, if it is within 4.5, 4, 5 which all 3 axies are.

1 Like

You can try and use .Magnitude

if (Part1.Position - Part2.Position).Magnitude < 10 then

Basically .Magnitude checks how far a part is from another part. What the script from above is saying is basically “is part1 less then 10 studs away from part2”

Here’s a video, I suggest you skip to 2:50 if you just want to see code

I misunderstood your point last time. Anyway: you’re gonna have to complete some maths to achieve this like others suggested.

Lets say the area is a rectangle of (30,10,10) and the centre is at (0,0,0) for simplicity.

local maximumAreaOffsets = Vector3.new(30/2,10/2,10/2)
local centreOfArea = Vector3.new(0,0,0)
local newPoint = Vector3.new(50,50,50)
local XoffsetFromArea = math.abs(newPoint.X-centreOfArea.X)
local YoffsetFromArea = math.abs(newPoint.Y-centreOfArea.Y)
local ZoffsetFromArea = math.abs(newPoint.Z-centreOfArea.Z)
--[[These are to get how far the point is from the core of the area so we can see if 
it is within it by comparing the offset with the specific lengths, 
it also checks if the offset is in the negatives so we can make it positive to make the next part easier.]]

if ZoffsetFromArea <= maximumAreaOffsets.Z and XoffsetFromArea <= maximumAreaOffsets.X and YoffsetFromArea <= maximumAreaOffsets.Y then
	print("within area")
else
	print("outside of area")
end

-- Since the point 50,50,50 is outside of a 30,10,10 rectangle, it prints outside. When we change the point to 15,5,5 it prints within.

This is the same as what CyanKiller suggested just with a code example.

You can use a function like this:

local function checkPointInArea(point: Vector3, area: Vector3, size: Vector3): boolean
	local distance = point - area
	local X = math.abs(distance.X)
	local Y = math.abs(distance.Y)
	local Z = math.abs(distance.Z)
	return (X < (size.X)/2) and (Y < (size.Y)/2) and (Z < (size.Z)/2)
end

local area = Instance.new("Part")
area.Parent = game.Workspace
area.Position = Vector3.new(5,5,5)
area.Size = Vector3.new(3, 6, 6)

local point = Vector3.new(6, 7, 5)

print(checkPointInArea(point, area.Position, area.Size)) -- true
1 Like

I tested it and it seems to be working, thank you for the code and explanation

This is actually exactly what I was looking for

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.