Trying to constraint placement outside of base, but it only works for 2 sides

Hello.

I am trying to setup a placement system and constraint it to the base.

But when I try to clamp the model it works for 2 sides, but not for the other 2 sides.

It’s hard to explain, so watch this gif.
GIF

Fyi, I am using ZBlock’s Placement System.

This is what I currently have. (It’s in the placement module):

local function CalcualateNewPosition()
	if MoveByGrid then
		posX = math.clamp(math.floor((mouse.Hit.X / grid) + 0.5) * grid, workspace.Plots.Plot1.Canvas.Position.X - workspace.Plots.Plot1.Canvas.Size.X / 2, workspace.Plots.Plot1.Canvas.Position.X + workspace.Plots.Plot1.Canvas.Size.X/2)
		posZ = math.clamp(math.floor((mouse.Hit.Z / grid) + 0.5) * grid, workspace.Plots.Plot1.Canvas.Position.Z - workspace.Plots.Plot1.Canvas.Size.Z / 2, workspace.Plots.Plot1.Canvas.Position.Z + workspace.Plots.Plot1.Canvas.Size.Z/2)
	else
		posX = mouse.Hit.X
		posZ = mouse.Hit.Z
	end
	
	if EnableFloors and not stacking then
		if posY > MaxHeight then
			posY = MaxHeight
		elseif posY < startingY then
			posY = startingY
		end
	end
	
	if stacking then
		posY = math.floor(mouse.Hit.Y) + step
		
		if posY > MaxHeight then
			posY = MaxHeight
		elseif posY < startingY then
			posY = startingY
		end
	end
end

I have used the clamping method from here: Stop part going outside an area? - #10 by StrategicPlayZ

I have tried multiple times, but It has not worked.

I have looked on the developer forum but I have not found a answer that works.

Thanks in advance.

local mDrag; mDrag = handle.MouseDrag:Connect(function(id, distance)
	local moving = handle.Adornee
	local axi = {
		[Enum.NormalId.Right] = moving.CFrame.RightVector,
		[Enum.NormalId.Left] = -moving.CFrame.RightVector,
		[Enum.NormalId.Top] = moving.CFrame.UpVector, 
		[Enum.NormalId.Bottom] = -moving.CFrame.UpVector,
		[Enum.NormalId.Front] = moving.CFrame.LookVector,
		[Enum.NormalId.Back] = -moving.CFrame.LookVector,
	}
	
	moving.CFrame = CF+axi[id]*(floor(distance/Increment)*Increment)
	local posX = clamp(moving.Position.X, (boundry.Position.X-boundry.Size.X/2)+moving.Size.X/2, (boundry.Position.X+boundry.Size.X/2)-moving.Size.X/2)
	local posY = clamp(moving.Position.Y, (boundry.Position.Y-boundry.Size.Y/2)+moving.Size.Y/2, (boundry.Position.Y+boundry.Size.Y/2)-moving.Size.Y/2)
	local posZ = clamp(moving.Position.Z, (boundry.Position.Z-boundry.Size.Z/2)+moving.Size.Z/2, (boundry.Position.Z+boundry.Size.Z/2)-moving.Size.Z/2)
	moving.Position = Vector3.new(posX, posY, posZ)
end)

This is how I did it. (A few changes from the original post.)

2 Likes