Better way of preventing players from placing outside of plot?

  1. What do you want to achieve? Keep it simple and clear!
    I want my placement system to prevent players from placing objects outside of their plot.

  2. What is the issue? Include screenshots / videos if possible!

Currently im just checking the X and Z positions to make sure theyre inside their plot. But sometimes it thinks a player is placing inside their plot when they arent.

Heres my current way of checking if theyre inside their plot:

Oh yeah heres the code that converts a CFrame to object space first:

function Convert_Cf(Base, Pos, Grid, T)
	local ObjPos = Base.CFrame:ToObjectSpace(Pos)
	
	local Cf = Calc_Cf(ObjPos, Grid, T) -- Basically just converts the CFrame into a 4x4x4 grid
	
	local WorldPos = Base.CFrame:ToWorldSpace(Cf * CFrame.Angles(0, math.rad(Current_obj.PrimaryPart.Orientation.Y), 0))
	
	
	return WorldPos
end

Thought that could be helpful before we continue down the road… Now to the main code:

local Dist_X = (Current_obj.PrimaryPart.Position.X - Plot.PrimaryPart.Position.X)
local Dist_Z = (Current_obj.PrimaryPart.Position.Z- Plot.PrimaryPart.Position.Z)
			
		
		
if Dist_X <= 52 and Dist_X >= - 52 and Dist_Z <= 53 and Dist_Z >= - 52 then
	Can_Place = true
else
	Can_Place = false
end

It works. But i mean sometimes it thinks theyre inside their plot like i already said. So is there any better way of detecting this kind of stuff?. Im not the best with math so yeahhhhh…

Perhaps you could test the X and Z properties of the mouse to determine if they’re both in bounds of your plot’s coordinates, and if they aren’t then don’t move the model to the mouse’s location.

Which really the logic should be simple:

local function isMouseInBoundsOfPlot()
local XMin, XMax = boundPos.X - (boundPos.Size.X / 2), boundPos.X + (boundPos.Size.X / 2)
local YMin, YMax = boundPos.Y - (boundPos.Size.Y / 2), boundPos.Y + (boundPos.Size.Y / 2)

return (mousePos.X >= XMin and mousePos.X <= XMax) and (mousePos.Y >= YMin and mousePos.Y <= YMax)
end
3 Likes

One thing to note about using the X and Z coordinates to determine if an object is in a plot: the X and Z coordinates are of the CENTER of the object. So if there is a large object, a player can easily make a lot of the object hang out of the plot boundaries because technically the object’s center is in the bounds of the plot.

Try Region3 or you can use more complex arithmetic to make sure the edges of the object are within the plot

1 Like

You could circumvent this by adding/subtracting half of the object’s size to the X and Z positional co-ordinates of the object and determining if that would exceed the specified boundaries.

Bare in mind, this will only work for quadrilaterals not for shapes with a more distorted form.

1 Like