Subtracting Vectors Not Working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to check if the distance between a part (whose Y is ALWAYS 0.5) and 0,0.5,0. If the distance is 0, then clone a model and use :PivotTo() to position it at the part.Spawn : : Part (2 colons define the Class of an object). But for some reason, it does not seem to be working.

  2. What is the issue? Include screenshots / videos if possible!

local DefaultStructures = script.Structures["Default Structures"] -- default structures (starter house at 0m, car barn at 0m)
local Structures = script.Structures.Structures

workspace.DescendantAdded:Connect(function(des) -- something got added to workspace
	if des:IsDescendantOf(workspace.RoadsFolder) and des:IsA("Model") then -- descendant is a road
		print(des.ClassName, des.Name)
			local clone = DefaultStructures["Starter House"]:Clone()
		clone.Parent = workspace
		print(des.Road.Position)
		if (Vector3.new(0,0.5,0) - des.Road.Position).Magnitude == 0 then -- road is the first one to be generated (aka its at 0m)
			
			
			clone:PivotTo(des:FindFirstChild("Road"):FindFirstChild("Spawn2").CFrame)
			
		--elseif 
			
		end
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried subtracting the position of the part from 0,0,0 instead, still nothing. Mind you that this is pairing with a Road Generation script to generate structures beside roads. The roads generate in the -Z direction from 0,0,0. If it helps, here’s the Road Generation script as well:
local roadLength = 35 -- Length of each road segment
local totalSegments = math.huge -- Total number of segments
local roadWidth = 65 -- Width of the road (default 65)
local roadHeight = 10 -- Thickness/height of the road part
local maxTurnAngle = 6 -- Maximum turn angle in degrees per segment for smooth curves
local maxDeviation = 20 -- Maximum allowed cumulative deviation before forcing a correction

local cumulativeTurn = 0 -- Track cumulative turns to correct large deviations
local correctionMode = false -- To determine if we're in correction mode
local correctionTarget = 0 -- The target to correct the cumulative turn

local lastRoad = 1 -- DO NOT CHANGE TO 2 OR STRUCTURE GENERATION SCRIPT WILL BREAK 

-- Store the last placed road's CFrame for precise positioning
local lastCFrame = CFrame.new(Vector3.new(0, -.5, 0), Vector3.new(0, -.5, -1)) -- Initially pointing along negative Z-axis

local segmentCount = 0 -- Keep track of how many segments have been created
local unionParts = {} -- Table to store primary parts for unioning

local currentIndex = 0

-- Create a function to generate the road
local function createRoad()
	for i = 1, totalSegments do
		local roadSegment
		
		if lastRoad == 1 then
			lastRoad = 2
			roadSegment = script.Road:Clone()
		else
			lastRoad = 1
			roadSegment = script.Road2:Clone()
		end

		currentIndex += 1
		roadSegment.Parent = workspace.RoadsFolder
		roadSegment.Name = "Road " .. currentIndex
		for _, v in roadSegment:GetChildren() do
			v.Anchored = true
			wait()
		end
		local turnAngle = 0
		if correctionMode then
			turnAngle = math.clamp(-cumulativeTurn, -maxTurnAngle, maxTurnAngle)
			cumulativeTurn = cumulativeTurn + turnAngle
			if math.abs(cumulativeTurn) <= 0.1 then
				correctionMode = false
				cumulativeTurn = 0
			end
		else
			local shouldCurve = math.random(1, 6) == 1
			if shouldCurve then
				turnAngle = math.random(-maxTurnAngle, maxTurnAngle)
				cumulativeTurn = cumulativeTurn + turnAngle
			end
			if math.abs(cumulativeTurn) > maxDeviation then
				correctionMode = true
			end
		end

		-- Apply the turn angle to the road segment
		local newCFrame = lastCFrame * CFrame.Angles(0, math.rad(turnAngle), 0)
		newCFrame = newCFrame + newCFrame.LookVector * roadLength -- Move forward by road length

		-- Set the road segment's CFrame to align it properly
		roadSegment:SetPrimaryPartCFrame(newCFrame)
		lastCFrame = newCFrame -- Update lastCFrame to this road segment's CFrame

		-- Add the PrimaryPart of the road segment to the table for unioning
		table.insert(unionParts, roadSegment.PrimaryPart)

		-- After 5 parts, union them together and set the pivot to the last road segment
		segmentCount = segmentCount + 1
		if segmentCount % 2 == 0 then
			local basePart = unionParts[1] -- Use the last (5th) road's primary part as the base for the union
			local unionedPart = nil

			pcall(function()
				-- Create the union with the pivot set to the last part
				unionedPart = basePart:UnionAsync({unpack(unionParts, 1, 1)}) -- Union the first 4 parts with the 5th
			end)

			-- If union is successful, configure and parent the unioned part
			if unionedPart then
				unionedPart.Parent = workspace.RoadsFolder
				unionedPart.Anchored = true

				-- Move the unioned part to the position of the last road segment (5th part's CFrame)
				unionedPart.CFrame = basePart.CFrame

				-- Clean up the original parts
				for _, part in ipairs(unionParts) do
					part.Parent:Destroy() -- Remove the entire model that contains the PrimaryPart
				end

				-- Set the lastCFrame to the pivot of the unioned part to continue the correct positioning
				lastCFrame = unionedPart.CFrame
			end

			-- Clear the unionParts table for the next batch
			unionParts = {}
		end
	end
end

-- Call the function to generate the road
createRoad()

Note: The structure generation script throws this error:

 Postion is not a valid member of Part "Workspace.RoadsFolder.Road 2.Road" 

Also, by the error i’m sure you can probably tell but even though the lastroad variable in the Road generation script is 1 (which is supposed to clone the road model called Road and not Road2 (not to be confused with Road 2, Road2 (with no space) is the name of the second type of road model while Road 2 with a space, here 2 is the number of roads generated. I’m not sure why it thinks that there are 2 roads generated where it’s supposed to be Road 1 (no model called Road 1 gets created).

Thank you in advance to anyone who can solve these few problems!

there is a typo here, it should be Position

1 Like

I changed it, still doesn’t work but it DOES NOT error anymore. Also, here’s the explorer if you need it:
image

What is not working exactly, the position,cloning,etc?

Could you elaborate when you say “still doesn’t work”? And do you think you could show us the objects in the workspace instead of only ServerScriptService?

I’ll send the .rbxl file so it’s easier and we don’t have to go back and forth.
l.rbxl (8.3 MB)

You could instead say what is wrong and we would help instead of checking a whole explorer for the problem.

I tried explaining it the best I could. The main problem here is positioning the cloned Starter House, which I can’t make it do.