- What do you want to achieve? Keep it simple and clear!
I want to make a road placement system like in this video
-
What is the issue? Include screenshots / videos if possible!
my code is messed up and it glitched only scaled to -x and x, not z or -z and also the position changed
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
here is my code
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local selectedPart = nil
local defaultSize = Vector3.new(4, 1, 4)
local previousDirection = nil
mouse.Button1Down:Connect(function()
local target = mouse.Target
if target and target:IsA("BasePart") then
if selectedPart then
selectedPart.SelectionBox:Destroy()
end
selectedPart = target
previousDirection = nil
local selectionBox = Instance.new("SelectionBox")
selectionBox.Adornee = selectedPart
selectionBox.Color3 = Color3.fromRGB(0, 255, 0)
selectionBox.Parent = selectedPart
end
end)
mouse.Move:Connect(function()
if selectedPart then
local mouseX = mouse.X
local mouseY = mouse.Y
local partPosition = selectedPart.Position
local camera = workspace.CurrentCamera
local unitRay = camera:ScreenPointToRay(mouseX, mouseY)
local planePos = unitRay.Origin + (unitRay.Direction * (partPosition.Y - unitRay.Origin.Y) / unitRay.Direction.Y)
local direction = planePos - partPosition
local currentDirection
if math.abs(direction.X) > math.abs(direction.Z) then
currentDirection = "X"
else
currentDirection = "Z"
end
if previousDirection and previousDirection ~= currentDirection then
local angle = 0
if math.abs(direction.X) > math.abs(direction.Z) then
if direction.X > 0 then
angle = 0 -- Facing positive X
else
angle = math.pi -- Facing negative X
end
else
if direction.Z > 0 then
angle = math.pi / 2 -- Facing positive Z
else
angle = -math.pi / 2 -- Facing negative Z
end
end
selectedPart.CFrame = CFrame.new(partPosition) * CFrame.Angles(0, angle, 0)
end
previousDirection = currentDirection
-- Calculate new size based on direction in steps of 1 stud along X axis
local newSizeX = selectedPart.Size.X
local partCFrame = selectedPart.CFrame
if currentDirection == "X" then
-- Scale in X direction in steps of 1 stud
local targetSizeX = math.ceil(math.abs(direction.X))
if targetSizeX % 2 ~= 0 then
targetSizeX = targetSizeX + 1
end
newSizeX = targetSizeX
-- Adjust the position to keep the part centered while resizing
local offsetX = (newSizeX - selectedPart.Size.X) / 2
partCFrame = partCFrame * CFrame.new(offsetX, 0, 0)
end
selectedPart.Size = Vector3.new(newSizeX, selectedPart.Size.Y, selectedPart.Size.Z)
selectedPart.CFrame = partCFrame
end
end)
mouse.Button2Down:Connect(function()
if selectedPart then
selectedPart.SelectionBox:Destroy()
selectedPart = nil
end
end)