Hello, currently I am making a Road Paver and I made script for making road. This script uses the part on road paver to get new X and Z position for road but Y stays the same. However visually I can see the difference in height.
This is Gyazo video below:
video
local body = script.Parent.Parent.Body
local asphaltValue = script.Parent.Parent:WaitForChild("Asphalt")
local makePart = body.AsphaltPart
local template = game:GetService("ServerStorage"):WaitForChild("template", 5)
local dirt = body["Dirt Particles"].Dirt
if not template then
warn("Template not found in ServerStorage!")
return
end
local paver = script.Parent.Parent
local partSize = 19.637
local lastPartPosition = nil
local function calculateDistance(pos1, pos2)
return (pos1 - pos2).Magnitude
end
local function unionAsphaltParts()
local folder = workspace:FindFirstChild("PavedRoads")
if not folder then
warn("PavedRoads folder not found in workspace")
return
end
local firstPart = folder:FindFirstChild("FirstAsphalt")
if not firstPart then
warn("FirstAsphalt part not found in PavedRoads folder")
return
end
local otherParts = {}
for _, part in ipairs(folder:GetChildren()) do
if part:IsA("BasePart") and part ~= firstPart then
table.insert(otherParts, part)
end
end
if #otherParts == 0 then
warn("No valid parts to union with FirstAsphalt")
return
end
local success, result = pcall(function()
return firstPart:UnionAsync(otherParts)
end)
if success then
result.Parent = folder
for _, part in ipairs(otherParts) do
part:Destroy()
end
else
warn("Union operation failed:", result)
end
end
local function placeNewPart()
if not lastPartPosition then
lastPartPosition = makePart.Position
end
local newPart = template:Clone()
local constantY = lastPartPosition.Y
newPart.Position = Vector3.new(makePart.Position.X, constantY, makePart.Position.Z)
newPart.Anchored = true
newPart.Orientation = makePart.Orientation
newPart.Parent = workspace.PavedRoads
print(string.format("New Part Position: X: %f, Y: %f, Z: %f", newPart.Position.X, newPart.Position.Y, newPart.Position.Z))
lastPartPosition = Vector3.new(newPart.Position.X, constantY, newPart.Position.Z)
end
script.Parent.OnServerEvent:Connect(function(player, Status)
if Status == true then
if asphaltValue.Value > 0 then
print("Paver has Asphalt, paving can start.")
if not lastPartPosition then
local firstPart = template:Clone()
firstPart.Position = makePart.Position
firstPart.Orientation = makePart.Orientation
firstPart.Anchored = true
firstPart.Parent = workspace.PavedRoads
firstPart.Name = "FirstAsphalt"
lastPartPosition = makePart.Position
asphaltValue.Value -= 1
end
while asphaltValue.Value > 0 and Status == true do
local currentPaverPosition = makePart.Position
dirt.Enabled = true
if calculateDistance(currentPaverPosition, lastPartPosition) >= 1 then
placeNewPart()
asphaltValue.Value -= 1
end
wait(0.05)
end
else
print("Paver is out of Asphalt.")
dirt.Enabled = false
body.Asphalt.Transparency = 1
end
elseif Status == false then
print("Paver has been stopped.")
dirt.Enabled = false
end
end)
asphaltValue.Changed:Connect(function(val)
if asphaltValue.Value == 0 then
print("Paver is out of Asphalt.")
dirt.Enabled = false
body.Asphalt.Transparency = 1
unionAsphaltParts()
end
end)
Placing new part function.
