function ClickToMoveDisplay.CreatePathDisplay(V,A)createPathCount=createPathCount+1 local T=createTrailDots(V,A)local function F(V)for A=#T,1,-1 do local F=T[A]if F.ClosestWayPoint<=V then F:Destroy()T[A]=nil else break end end end local n="\067\108\105\099\107\084\111\077\111\118\101\082\101\115\105\122\101\084\114\097\105\108"..createPathCount local function j()if#T==0 then RunService:UnbindFromRenderStep(n)return end local V=Workspace.CurrentCamera.CFrame.p for A=1,#T,1 do local F=T[A].DisplayModel:FindFirstChild("\084\114\097\105\108\068\111\116\073\109\097\103\101")if F then local n=(T[A].DisplayModel.Position-V).magnitude F.Size=getTrailDotScale(n,TrailDotSize)end end end RunService:BindToRenderStep(n,Enum.RenderPriority.Camera.Value-1,j)local function s()F(#V)end return s,F end local V=nil function ClickToMoveDisplay.DisplayFailureWaypoint(A)if V then V:Hide()end local T=FailureWaypoint.new(A)V=T;(coroutine.wrap(function()T:RunFailureTween()T:Destroy()T=nil end))()end function ClickToMoveDisplay.CreateEndWaypoint(V)return EndWaypoint.new(V)end function ClickToMoveDisplay.PlayFailureAnimation()local V=findPlayerHumanoid()if V then local A=getFailureAnimationTrack(V)A:Play()end end function ClickToMoveDisplay.CancelFailureAnimation()if lastFailureAnimationTrack~=nil and lastFailureAnimationTrack.IsPlaying then lastFailureAnimationTrack:Stop()end end function ClickToMoveDisplay.SetWaypointTexture(V)TrailDotIcon=V TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetWaypointTexture()return TrailDotIcon end function ClickToMoveDisplay.SetWaypointRadius(V)TrailDotSize=Vector2.new(V,V)TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetWaypointRadius()return TrailDotSize.X end function ClickToMoveDisplay.SetEndWaypointTexture(V)EndWaypointIcon=V TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetEndWaypointTexture()return EndWaypointIcon end function ClickToMoveDisplay.SetWaypointsAlwaysOnTop(V)WaypointsAlwaysOnTop=V TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetWaypointsAlwaysOnTop()return WaypointsAlwaysOnTop end return ClickToMoveDisplay

function ClickToMoveDisplay.CreatePathDisplay(wayPoints, originalEndWaypoint)
	createPathCount = createPathCount + 1
	local trailDots = createTrailDots(wayPoints, originalEndWaypoint)

	local function removePathBeforePoint(wayPointNumber)
		-- kill all trailDots before and at wayPointNumber
		for i = #trailDots, 1, -1 do
			local trailDot = trailDots[i]
			if trailDot.ClosestWayPoint <= wayPointNumber then
				trailDot:Destroy()
				trailDots[i] = nil
			else
				break
			end
		end
	end

	local reiszeTrailDotsUpdateName = "ClickToMoveResizeTrail" ..createPathCount
	local function resizeTrailDots()
		if #trailDots == 0 then
			RunService:UnbindFromRenderStep(reiszeTrailDotsUpdateName)
			return
		end
		local cameraPos = Workspace.CurrentCamera.CFrame.p
		for i = 1, #trailDots do
			local trailDotImage = trailDots[i].DisplayModel:FindFirstChild("TrailDotImage")
			if trailDotImage then
				local distanceToCamera = (trailDots[i].DisplayModel.Position - cameraPos).magnitude
				trailDotImage.Size = getTrailDotScale(distanceToCamera, TrailDotSize)
			end
		end
	end
	RunService:BindToRenderStep(reiszeTrailDotsUpdateName, Enum.RenderPriority.Camera.Value - 1, resizeTrailDots)

	local function removePath()
		removePathBeforePoint(#wayPoints)
	end

	return removePath, removePathBeforePoint
end

local lastFailureWaypoint = nil
function ClickToMoveDisplay.DisplayFailureWaypoint(position)
	if lastFailureWaypoint then
		lastFailureWaypoint:Hide()
	end
	local failureWaypoint = FailureWaypoint.new(position)
	lastFailureWaypoint = failureWaypoint
	coroutine.wrap(function()
		failureWaypoint:RunFailureTween()
		failureWaypoint:Destroy()
		failureWaypoint = nil
	end)()
end

function ClickToMoveDisplay.CreateEndWaypoint(position)
	return EndWaypoint.new(position)
end

function ClickToMoveDisplay.PlayFailureAnimation()
	local myHumanoid = findPlayerHumanoid()
	if myHumanoid then
		local animationTrack = getFailureAnimationTrack(myHumanoid)
		animationTrack:Play()
	end
end

function ClickToMoveDisplay.CancelFailureAnimation()
	if lastFailureAnimationTrack ~= nil and lastFailureAnimationTrack.IsPlaying then
		lastFailureAnimationTrack:Stop()
	end
end

function ClickToMoveDisplay.SetWaypointTexture(texture)
	TrailDotIcon = texture
	TrailDotTemplate, EndWaypointTemplate, FailureWaypointTemplate = CreateWaypointTemplates()
end

function ClickToMoveDisplay.GetWaypointTexture()
	return TrailDotIcon
end

function ClickToMoveDisplay.SetWaypointRadius(radius)
	TrailDotSize = Vector2.new(radius, radius)
	TrailDotTemplate, EndWaypointTemplate, FailureWaypointTemplate = CreateWaypointTemplates()
end

function ClickToMoveDisplay.GetWaypointRadius()
	return TrailDotSize.X
end

function ClickToMoveDisplay.SetEndWaypointTexture(texture)
	EndWaypointIcon = texture
	TrailDotTemplate, EndWaypointTemplate, FailureWaypointTemplate = CreateWaypointTemplates()
end

function ClickToMoveDisplay.GetEndWaypointTexture()
	return EndWaypointIcon
end

function ClickToMoveDisplay.SetWaypointsAlwaysOnTop(alwaysOnTop)
	WaypointsAlwaysOnTop = alwaysOnTop
	TrailDotTemplate, EndWaypointTemplate, FailureWaypointTemplate = CreateWaypointTemplates()
end

function ClickToMoveDisplay.GetWaypointsAlwaysOnTop()
	return WaypointsAlwaysOnTop
end

return ClickToMoveDisplay
