Block Placing System Troubles

function GetClosestMultiple(x)
	local floorMultiple = math.floor(x / 4) * 4
	local ceilMultiple = math.ceil(x / 4) * 4

	local closestMultiple
	if math.abs(x - floorMultiple) < math.abs(x - ceilMultiple) then
		closestMultiple = floorMultiple
	else
		closestMultiple = ceilMultiple
	end

	print("Original:", x, "Closest multiple of 4:", closestMultiple)
	return closestMultiple
end

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
	local pos = mouse.Hit.Position
	local x = pos.X
	local y = pos.Y
	local z = pos.Z

	local newX = GetClosestMultiple(x)
	local newY = GetClosestMultiple(y)
	local newZ = GetClosestMultiple(z)

	local Pos = Vector3.new(newX, newY, newZ)
	print("New block position: "..newX..", "..newY..", "..newZ)

	if game.Workspace.ClientStorage:FindFirstChild("MoveToPart") then
		game.Workspace.ClientStorage.MoveToPart:Destroy()
	end

	local mtp = script.MoveToPart:Clone()
	mtp.Position = Pos
	mtp.Parent = game.Workspace.ClientStorage
end)

Image of me placing on -X

Image of me placing on positive X

So for some reason when I try placing on the - side of X, Y and Z it goes inside the block I’m trying to place.

nevermind, changed it a lot. i watched a tutorial.

function GetClosestMultiple(x)
	--[[local floorMultiple = math.floor(x / 4) * 4
	local ceilMultiple = math.ceil(x / 4) * 4

	local closestMultiple
	if math.abs(x - floorMultiple) < math.abs(x - ceilMultiple) then
		closestMultiple = floorMultiple
	else
		closestMultiple = ceilMultiple
	end

	print("Original:", x, "Closest multiple of 4:", closestMultiple)]]--
	
	local closestMultiple = x // 4 * 4
	return closestMultiple
end

local castParams = RaycastParams.new()
castParams:AddToFilter(script.Parent)


local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
	local mouselocation = game:GetService("UserInputService"):GetMouseLocation()
	local unitray = workspace.CurrentCamera:ViewportPointToRay(mouselocation.X, mouselocation.Y)
	
	local pos = workspace:Raycast(unitray.Origin, unitray.Direction * 1000, castParams)
	local x = pos.Position.X
	local y = pos.Position.Y
	local z = pos.Position.Z

	local newX = GetClosestMultiple(x)
	local newY = GetClosestMultiple(y)
	local newZ = GetClosestMultiple(z)

	local PlacePos = Vector3.new(newX, newY, newZ)
	print("New block position: "..newX..", "..newY..", "..newZ)

	if game.Workspace.ClientStorage:FindFirstChild("MoveToPart") then
		game.Workspace.ClientStorage.MoveToPart:Destroy()
	end

	local mtp = script.MoveToPart:Clone()
	
	local normalcf = CFrame.lookAlong(pos.Position, pos.Normal)
	local relsnap = normalcf:PointToObjectSpace(PlacePos)
	local xVector = normalcf:VectorToWorldSpace(Vector3.xAxis * -math.sign(relsnap.X))
	local yVector = normalcf:VectorToWorldSpace(Vector3.yAxis * -math.sign(relsnap.Y)) 
	
	local cf = CFrame.fromMatrix(PlacePos, xVector, yVector, pos.Normal)
	
	mtp.Position = cf:PointToWorldSpace(mtp.Size/2)
	mtp.Parent = game.Workspace.ClientStorage
end)

heres the messy code if you want it

Could you please explain what the issue is?

I already fixed it. Thanks anyways.

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