Problem with plot boundaries and rotation

Hello, I have stumbled upon a problem with my placing with my placement system and that is with rotation. I’ve tried a variety of different ways to calculate the bounds but it seems to be buggy at times and I cannot figure anything else out on what would fix this. If you have any ideas or suggestions, that would be very much appreciated!

I’ve taken a look at a few Placement articles already including the one by EgoMoose but that did not fix my problem.

New Method: https://gyazo.com/bccfb8c3c357fd381955946ef168345e

Old Method: https://gyazo.com/bccfb8c3c357fd381955946ef168345e

Old:

local modelSize = CFrame.fromEulerAnglesYXZ(0, Rotation, 0) * Object.PrimaryPart.Size
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local ModelSize = (Vector3.new(modelSize.x, modelSize.y, modelSize.z))/2
	local ModelPos = Object.PrimaryPart.Position
	
	local PlotSize = self.Plot.Size/2
	local PlotPos = self.Plot.Position
	
	local ModelPos = Object.PrimaryPart.Position
	
	local Minimum = PlotPos - PlotSize
	local Maximum = PlotPos + PlotSize
	local ObjMass = Object.PrimaryPart:GetMass()
	local Z = math.abs(Rotation/PlotSize.Z/PlotPos.Z/ObjMass)
	local X = math.abs(Rotation/PlotSize.X/PlotPos.X/ObjMass)
	
	if Rotation > 0 then
		Z = math.floor(Rotation/PlotSize.Z/PlotPos.Z/ObjMass + .5) + (Z) / ModelPos.Z * ModelSize.Z
		X = math.floor(Rotation/PlotSize.X/PlotPos.X/ObjMass + .5) + (X) / ModelPos.X * ModelSize.X - 1
	else
		Z = math.floor(Rotation/PlotSize.Z/PlotPos.Z/ObjMass + .5) + (Z) / ModelPos.Z * ModelSize.Z + 2
		X = math.floor(Rotation/PlotSize.X/PlotPos.X/ObjMass + .5) + (X) / ModelPos.X * ModelSize.X + 2
	end
	
	local WithinX = ModelPos.X - ModelSize.X  >= (Minimum.X) + X and ModelPos.X + ModelSize.X <= (Maximum.X) - X
	local WithinY = ModelPos.Y - ModelSize.Y  >= 0 and ModelPos.Y + ModelSize.Y <= Maximum.Y + self.MaxHeight
	local WithinZ = ModelPos.Z - ModelSize.Z  >= (Minimum.Z) - Z and ModelPos.Z + ModelSize.Z <= (Maximum.Z) + Z 
	
	Object:SetPrimaryPartCFrame(HitWorldSnap * CFrame.Angles(0, math.rad(self.Rotation), 0))

New:

local CanvSize = (Vector3.new(self.Plot.Size.X, self.Plot.Size.Y, self.Plot.Size.Z))/2
	local CanvPos = self.Plot.CFrame:ToObjectSpace(CFrame.new(self.Plot.Position.X, self.Plot.Position.Y,self.Plot.Position.Z))

	local modelSize = CFrame.fromEulerAnglesYXZ(0, Rotation, 0) * Object.PrimaryPart.Size
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local ModelSize = (Vector3.new(modelSize.x, modelSize.y, modelSize.z))/2
	local ModelPos = Object.PrimaryPart.Position
	
	local PlotSize = self.Plot.Size/2
	local PlotPos = self.Plot.Position
	
	local ModelPos = Object.PrimaryPart.Position
	
	local Minimum = CanvPos - CanvSize
	local Maximum = CanvPos + CanvSize
	local ObjMass = Object.PrimaryPart:GetMass()
	local Z = math.abs(Rotation/PlotSize.Z/PlotPos.Z/ObjMass)
	local X = math.abs(Rotation/PlotSize.X/PlotPos.X/ObjMass)
	
	if Rotation > 0 then
		Z = math.floor(Rotation/PlotSize.Z/PlotPos.Z/ObjMass + .5) + (Z) / ModelPos.Z * ModelSize.Z
		X = math.floor(Rotation/PlotSize.X/PlotPos.X/ObjMass + .5) + (X) / ModelPos.X * ModelSize.X
	else
		Z = math.floor(Rotation/PlotSize.Z/PlotPos.Z/ObjMass + .5) + (Z) / ModelPos.Z * ModelSize.Z
		X = math.floor(Rotation/PlotSize.X/PlotPos.X/ObjMass + .5) + (X) / ModelPos.X * ModelSize.X
	end
	
	local WithinX = ModelPos.X - ModelSize.X  >= (Minimum.X) - X and ModelPos.X + ModelSize.X <= (Maximum.X) + X
	local WithinY = ModelPos.Y - ModelSize.Y  >= 0 and ModelPos.Y + ModelSize.Y <= Maximum.Y + self.MaxHeight
	local WithinZ = ModelPos.Z - ModelSize.Z  >= (Minimum.Z) - Z and ModelPos.Z + ModelSize.Z <= (Maximum.Z) + Z 
	
	Object:SetPrimaryPartCFrame(HitWorldSnap * CFrame.Angles(0, math.rad(self.Rotation), 0))

If anyone has any questions feel free to ask.