How could I create zones?

I am trying to make a building system but right now I am in a mental block of how to achieve this, I want to make a “plot” or “building area” but with the prospect i have of the game, the zone/building area will often be a irregular shape

reason I cant just use touched or check if something is inside the redsquares is because I need to reference the irregulars shape and position often when i make a build preview.,

Any help? advice?

2 Likes

You might be able to Raycast along the edges of the boxes and essentially use those to detect it?

You can probably outline the entire zone by using the intersection points to create new Raycast reference points.

I’m not entirely sure what the point is, so if I could get a little bit more context for what this is for, I’d be able to help out more.

1 Like

image

Ive been remaking my build system and it uses a math formula which i attached above, i could try to experiment with different methods but I dont know about raycasting for every check seems costly even for the client

As you can see the CFrame and Size for the plot only consider one “plot” at the moment

Im just trying to add a check for my preview that involves more plots that would ultimately create an irregular shape, if you have other suggestions that I could experiment with thatd also be great

2 Likes

I’ve done something before (if it will have square edges like that AND will not change in shape) you can store the boundaries in a module’s table, and use math to determine whether the object or whatever is inside. If the boundaries don’t need to change size or whatever that could work?

1 Like

ahh interesting take i love this ill try what you said and see how that works, i think i can maybe adapt it so if the boundaries changes it adjusts accordingly

1 Like

This looks like it should work, assuming you perform this check for each box in the zone.

If it isn’t though, loop through each zone with this function and if it ever returns true, break because target is within zone. Otherwise, if every check is false, they’re not inside.

2 Likes

I’d recommend using SimpleZone by @athar_adv

If for some reason you don’t want to use SimpleZone, I’d like to ask why aren’t you using workspace:GetPartsInPart()

1 Like

This is actually something I did in one of my projects. It worked pretty well. :+1:

Use a for loop to loop through all of those parts and then do workspace:GetPartsInPart() on the part and then add the rest of the checks there.

GetPartsInPart returns a table of instances which are inside the given basepart, there’s also a second optional parameter for an OverlapParams object.

2 Likes
local function is_in_some_shape(cframe : CFrame, shapeStuff)
	cframe = cframe:Inverse()
	return function(pos : Vector3)
		pos = cframe * pos
		-- check position to see if it is in bounds of your shape
		-- return true or false
	end
end

I think you just localize the position and check if it’s inside the shape right?

3 Likes

Your code allows the 3D rectangular prism to be in any orientation and size. I don’t know if you realize that your drawing is all squared up. It can be any rotation.

2 Likes

What are you checking for. Is this to be a touch thing or do you just need to know if it’s over an area?
Will this need to check all the time or when called on?

its checking if a build preview is within bounds, and its called everytime the mouse moves so.

Well, with the information given, I have no idea how you’re moving the “build” around. I’ll assume it’s based on the mouse position, and you can refine this script using the build’s primary part if needed.

(I’m assuming by “build,” you mean a Module.)

This is an unusual setup, but I like it. You can even use it to determine your zone dimensions generically. I’m placing box Parts inside a folder named “Zones” in the workspace. Together, they match your diagram, so there are four. These serve as zone references and should be transparent, with no shadows, locked, anchored, and massless. No behavior or collision settings. It will handle the “zones” mathematically from these parts, keeping it minimal and update-friendly.

Zone Script

This is a LocalScript in StarterGui … Just to test it.

local rns = game:GetService("RunService")
local zones = workspace:WaitForChild("Zones")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local squares = {}

for _, part in pairs(zones:GetChildren()) do
	if part:IsA("Part") then
		table.insert(squares, part)
	end
end

if #squares == 0 then return end
local minPos = squares[1].Position - squares[1].Size / 2
local maxPos = squares[1].Position + squares[1].Size / 2

for _, square in ipairs(squares) do
	local squareMin = square.Position - square.Size / 2
	local squareMax = square.Position + square.Size / 2
	minPos = Vector3.new(math.min(minPos.X, squareMin.X), math.min(minPos.Y, squareMin.Y), math.min(minPos.Z, squareMin.Z))
	maxPos = Vector3.new(math.max(maxPos.X, squareMax.X), math.max(maxPos.Y, squareMax.Y), math.max(maxPos.Z, squareMax.Z))
end

local function isInZone(build)
	local buildMin = build.Position - build.Size / 2
	local buildMax = build.Position + build.Size / 2
	return buildMin.X >= minPos.X and buildMax.X <= maxPos.X and
		buildMin.Y >= minPos.Y and buildMax.Y <= maxPos.Y and
		buildMin.Z >= minPos.Z and buildMax.Z <= maxPos.Z
end

mouse.Move:Connect(function()
	local target = mouse.Target
	if target and target:IsA("Part") then
		if isInZone(target) then
			print("mouse is inside the zone.")
		else
			print("mouse is outside the zone.")
		end
	end
	
	rns.Stepped:Wait()
end)
Print Zones
--paste before  function isInZone(build)
--in case you would like to try a Region3 approach

for _, square in ipairs(squares) do
	print(string.format("Zone: Pos(%.1f, %.1f, %.1f) Size(%.1f, %.1f, %.1f)", 
		square.Position.X, square.Position.Y, square.Position.Z, 
		square.Size.X, square.Size.Y, square.Size.Z))
end

This is more of an angle on how you could do this… In a somewhat testing phase.

1 Like