Detecting if a part is on a building plot

I’m making a building game, and each player will have their own plot (like the one shown above)

I need to detect if the part being placed is fully on the plot or not. My first thought would be to use Region3, but that’s been deprecated. I tried making a function which takes the “part” and “plot”, then returning “true” if the part is on the plot, and returning “false” if the part is not on the plot.

This is what I’ve got so far, I’ve made a table of all corners on the plot and object.

function isObjectOnPlot(object, plot)
	local ObjectDim = object.Size/2
	local PlotDim = plot.Size/2
	
	local ObjectCorners = { -- Table of all the corners' positions on the object
		object.CFrame * CFrame.new(ObjectDim.X, ObjectDim.Y, ObjectDim.Z),
		object.CFrame * CFrame.new(ObjectDim.X, ObjectDim.Y, -ObjectDim.Z),
		object.CFrame * CFrame.new(ObjectDim.X, -ObjectDim.Y, ObjectDim.Z),
		object.CFrame * CFrame.new(ObjectDim.X, -ObjectDim.Y, -ObjectDim.Z),
		object.CFrame * CFrame.new(-ObjectDim.X, ObjectDim.Y, ObjectDim.Z),
		object.CFrame * CFrame.new(-ObjectDim.X, ObjectDim.Y, -ObjectDim.Z),
		object.CFrame * CFrame.new(-ObjectDim.X, -ObjectDim.Y, ObjectDim.Z),
		object.CFrame * CFrame.new(-ObjectDim.X, -ObjectDim.Y, -ObjectDim.Z)
	}
	
	local PlotCorners = { -- Table of all corners' positions on the plot
		plot.CFrame * CFrame.new(PlotDim.X, PlotDim.Y, PlotDim.Z),
		plot.CFrame * CFrame.new(PlotDim.X, PlotDim.Y, -PlotDim.Z),
		plot.CFrame * CFrame.new(PlotDim.X, -PlotDim.Y, PlotDim.Z),
		plot.CFrame * CFrame.new(PlotDim.X, -PlotDim.Y, -PlotDim.Z),
		plot.CFrame * CFrame.new(-PlotDim.X, PlotDim.Y, PlotDim.Z),
		plot.CFrame * CFrame.new(-PlotDim.X, PlotDim.Y, -PlotDim.Z),
		plot.CFrame * CFrame.new(-PlotDim.X, -PlotDim.Y, PlotDim.Z),
		plot.CFrame * CFrame.new(-PlotDim.X, -PlotDim.Y, -PlotDim.Z)
	}
end

I’m sure you can use the corners of the plot to detect if the object is on the plot or not, but I’m not sure how to do that

Assuming your plot part is always a single part and not a model, or using the “base” part inside a model, the following code works for both single parts or models. This assumes that your Y axis is not valid and you only care if it’s between the X and Z dimensions. If you want a Y dimension, you’d need another check.

I used warn() statements to avoid the object test returning true/false before the model check ran, but you can use return true or return false in place of warn() or whatever code you might need.

function isObjectOnPlot(model, obj, plot)
	local objDimX, objDimZ = obj.Size.X, obj.Size.Z
	local plotDimX, plotDimZ = plot.Size.X, plot.Size.Z
	
	local modelVector = model:GetExtentsSize()
	local modelCFrame = model:GetBoundingBox()
	local modelDimX, modelDimZ = modelVector.X, modelVector.Z
	
	-- Test Object X and Z
	if (obj.CFrame.X <= ((plot.CFrame.X + plotDimX / 2) - objDimX / 2) and obj.CFrame.X >= ((plot.CFrame.X - plotDimX / 2) + objDimX / 2)) and (obj.CFrame.Z <= ((plot.CFrame.Z + plotDimZ / 2) - objDimZ / 2) and obj.CFrame.Z >= ((plot.CFrame.Z - plotDimZ / 2) + objDimZ / 2)) then
		warn("Object: True")
	else
		warn("Object: False")
	end
	-- Test Model X and Z
	if (modelCFrame.X <= ((plot.CFrame.X + plotDimX / 2) - modelDimX / 2) and modelCFrame.X >= ((plot.CFrame.X - plotDimX / 2) + modelDimX / 2)) and (modelCFrame.Z <= ((plot.CFrame.Z + plotDimZ / 2) - modelDimZ / 2) and modelCFrame.Z >= ((plot.CFrame.Z - plotDimZ / 2) + modelDimZ / 2)) then
		warn("Model: True")
	else
		warn("Model: False")
	end
end
obj.CFrame.X <= ((plot.CFrame.X + plotDimX / 2) - objDimX / 2)
-- Ex: ((0 + (10 / 2) - 4 / 2) then the obj.Cframe.X must be less than 
-- or equal to 3 otherwise it is over the edge 
-- then repeat for 3 other sides (-x, +z, -z)
1 Like

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