Detecting if the player is within certain points

I want to make a script that detects if you’re within certain boundaries, using parts as the points of the boundary.

However, I have no clue on how to go about this.

The boundary is within these points shown below:

4 Likes

While you could do some weird math to check if a player is within an arbitrary set of 3D points, why not just make a set of invisible parts, perhaps unioned, and check if the player is touching them?

Because I don’t really trust roblox’s physics too well

You don’t have to, call :GetTouchingParts() and see if a hitbox part is in the table.

I’ll try it

1 Like

Just make sure to understand the weird caveat of :GetTouchingParts() in that it only reliably shows you parts that have CanCollide true. The easiest way to do this is:

hitboxUnionThing.CanCollide = true
for _,part in pairs(HumanoidRootPart:GetTouchingParts()) do
    if part == hitboxUnionThing then
        --bla bla bla
        break
    end
end
hitboxUnionThing.CanCollide = false

See if it works without doing the dumb CanCollide thing, but if it doesn’t show up in the table that’s how you’d fix it.

2 Likes

From my experience, you can leave the block CanCollide=true, and put it in a collision group which won’t collide with the character.

2 Likes

That makes sense and would probably work here, thanks for the tip.

Here’s some code I shared a while ago that solves this

local function PointInPolygon(Point, Polygon)
	local current = Polygon
	local inside = false
	local A = current.Position
	local ax, az = A.X, A.Z
	local px, pz = Point.X, Point.Z
	repeat
		local B = current.Next.Position
		local bx, bz = B.X, B.Z
		if ((az >= pz) ~= (bz >= pz)) and ((px - ax) <= (bx - ax)*(pz - az)/(bz - az)) then
			inside = not inside
		end
		current = current.Next
		A, ax, az = B, bx, bz
	until current == Polygon
	return inside
end

local function CreatePolygonFromPoints(Points)
	local Polygon = {Position = Points[1]}
	local First = Polygon
	for i = 2, #Points do
		Polygon.Next = {Previous = Polygon, Position = Points[i]}
		Polygon = Polygon.Next
	end
	Polygon.Next, First.Previous = First, Polygon
	return First
end

CreatePolygonFromPoints takes a table of Vector3s and returns a “Polygon”, which is a circular doubly linked list of vertices. PointInPolygon takes a point and a polygon and returns whether the point is inside that polygon.

14 Likes

I’ve been getting this error https://gyazo.com/9d32dccc4e29bf7cf50b449b3dc1b693. I don’t know why this error is happening.

Here’s the code that I’m using for it: https://pastebin.com/SLzcdTJd

Some alternative code. Points should be an array of Vector3s in order. Will only work if the area defined by the Points is convex. (Written on mobile, untested).

local function PointWithinArea(Point, Points)
    local Side
    for i = 1, #Points do
        local Point0 = Points[i]
        local Point1 = Points[i == #Points and 1 or i + 1]
        local Value = (Point1 - Point0):Cross(Point - Point0).Y > 0
        if Side ~= nil and Value ~= Side then
            return false
        end
        Side = Value
    end
    return true
end
12 Likes

Worked without having to debug anything. Thank you very much!

2 Likes