Building System won't Allign at Certain Position?

I’m working on a a building system and I encountered a problem that’s making me go insane. When building upwards and then building out from the tower i created, building out only works from two sides. When I attempt to build out from the other sides, the preview remains inside of the block I’m trying to build from instead of going outward.

The blue dots represent where my mouse was when I clicked to paste the part and are part of me trying to debug.

image


local GridSize = 5
local HeightLock = 2
local HeightLockEnabled = false

local function AlignGrid(Position)

	local function A(a)

		local AB = math.floor(a/GridSize + 0.5) * GridSize
		return AB

	end

	local C = math.floor(Position.Y/GridSize + 0.5) 
	C = C * GridSize

	if HeightLockEnabled then
		C = HeightLock * GridSize
	end

	local Alligned = Vector3.new(A(Position.X),C,A(Position.Z))

	return Alligned
end


local function PreviewObject()

	while true do
		local Check = true
		if not PreviewActive then 
			Check = false
		elseif not Preview then
			Check = false
		end

		if Check then
			wait()

			Mouse.TargetFilter = Preview
			local MousePos = Mouse.Hit.Position
			local A =  AlignGrid(MousePos)
			Preview.Position = A
		end

	end

end

coroutine.wrap(PreviewObject)()

1 Like

Don’t use the mouse’s hit position. Add the surface normal * half the block size to it, then round it.

You are a lifesaver! Thank you so much!

1 Like

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