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)
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)