Orientating a line correctly inbetween two positions

Hey can someone help me with a road drawing system I’m making? Its supposed to draw the roads in the direction of the red line but for some reason the orientation doesn’t work.

local FlagPlacementPos = nil

game.ReplicatedStorage.Signals.UpdateFlagPlacement.OnServerEvent:Connect(function(plr, pos)
	FlagPlacementPos = pos.p
end)

script.Parent.Activated:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	
	local ray = Ray.new(FlagPlacementPos, Vector3.new(0, -20, 0))
	local ignoreinstance = workspace.Water
	local part, position = workspace:FindPartOnRay(ray, ignoreinstance, false, true)
	
	--print("Activated")
	if part ~= nil then
		--print("Part not nil..part name is "..part.Name)
		if part:FindFirstAncestor("World Map") then
			--print("Part is ancestor of world map")
			local flag = game.ReplicatedStorage.GameObject.CaptureFlag:Clone()
			flag.Parent = workspace
			flag.Name = plr.Name.." Flag"
			flag.Flag.g.Frame.t.Text = plr.Name
			flag:SetAttribute("FlagOwner", plr.Name)
			flag.Pole.Color = plr.playerdata.PlayerColor.Value
			flag.Flag.g.Frame.t.TextColor3 = plr.playerdata.PlayerColor.Value
			flag.Flag.g.Frame.UIStroke.Color = plr.playerdata.PlayerColor.Value
			flag:SetPrimaryPartCFrame(CFrame.new(position.X, position.Y + 2, position.Z))
			script.Parent:Destroy()
			flag.Radius.CanCollide = true
			local parts = flag.Radius:GetTouchingParts()
			game.ReplicatedStorage.Signals.RemoveFlag:FireClient(plr)
			for _, p in pairs(parts) do
				if p:FindFirstAncestor("World Map") then
					if not p:GetAttribute("Ruler") or p:GetAttribute("Ruler") == nil then
						p.Color = plr.playerdata.PlayerColor.Value
						p:SetAttribute("Ruler", plr.Name)
					end
				end
			end
			flag.Radius:Destroy()
		end
	end
end)

Above is the script used. If anyone could help that would be greatly appreciated!

1 Like

This is pretty much just visualizing a ray, right? Try this:

1 Like

Here you’re only defining the Position of the Part, you’ll also need to define the rotation. There is one CFrame constructor that lets you create a CFrame with a Position like normal, but whose rotation is such that the CFrame points towards a different position. But you’ll need to determine which position that is. From what you posted it’s not clear how you want the player to indicate the orientation of the road?

By moving the mouse. I’d want it to be rotated based on where the mouse is relative to the origin (the flag)

I see. You’d have to update it every RenderStepped. If you want to place the part between two points A and B (e.g. the first point that was clicked and whatever the mouse position is on subsequent frames), you can do that like so:

part.CFrame = CFrame.new((A + B)/2, B)
part.Size = part.Size * Vector3.new(1, 1, 0) + Vector3.new(0, 0, (B - A).Magnitude)

Oh yeah sorry forgot to reply, I fixed the problem by looking around the devforum but thanks for the help

1 Like