Height doesn't stay same while placing new part

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.

Can you please confirm that the heights and y size of the parts are the same? (Use the explorer while ingame to inspect the parts.)

I’m not seeing anything immediately wrong with the script, so checking that an issue is occuring (beyond just visually) is important.

Trough printing I am checking the height, here is the print:
New Part Position: X: -98.411560, Y: 0.103134, Z: 67.492661 - Server - Script:81
00:25:18.123 New Part Position: X: -99.221703, Y: 0.103134, Z: 68.086472 - Server - Script:81
00:25:18.383 New Part Position: X: -100.140999, Y: 0.103134, Z: 68.753189 - Server - Script:81

have you tried setting newPart.CFrame rather than newPart.Position? assuming they all have the same Y value for size, if they’re overlapping and you’re using basepart.Position, then they’re likely being offset

Just to confirm - are the parts anchored? (Gotta check all possibilities)

So instead of Vector I use CFrame right?

Yes, all parts are anchored and also I turned the collision off

yes, my guess is that’s most likely the issue. when you use CFrame, it moves the basepart to the exact coordinate frame without any overlap checking (unless overlapping part(s) are unanchored)

Well it worked, its going smooth as butter. Thank you

1 Like

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