function ClickToMoveDisplay.CreatePathDisplay(S,z)createPathCount=createPathCount+1 local m=createTrailDots(S,z)local function r(S)for z=#m,1,-1 do local r=m[z]if r.ClosestWayPoint<=S then r:Destroy()m[z]=nil else break end end end local Y="\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 O()if#m==0 then RunService:UnbindFromRenderStep(Y)return end local S=Workspace.CurrentCamera.CFrame.p for z=1,#m,1 do local r=m[z].DisplayModel:FindFirstChild("\084\114\097\105\108\068\111\116\073\109\097\103\101")if r then local Y=(m[z].DisplayModel.Position-S).magnitude r.Size=getTrailDotScale(Y,TrailDotSize)end end end RunService:BindToRenderStep(Y,Enum.RenderPriority.Camera.Value-1,O)local function G()r(#S)end return G,r end local S=nil function ClickToMoveDisplay.DisplayFailureWaypoint(z)if S then S:Hide()end local m=FailureWaypoint.new(z)S=m;(coroutine.wrap(function()m:RunFailureTween()m:Destroy()m=nil end))()end function ClickToMoveDisplay.CreateEndWaypoint(S)return EndWaypoint.new(S)end function ClickToMoveDisplay.PlayFailureAnimation()local S=findPlayerHumanoid()if S then local z=getFailureAnimationTrack(S)z:Play()end end function ClickToMoveDisplay.CancelFailureAnimation()if lastFailureAnimationTrack~=nil and lastFailureAnimationTrack.IsPlaying then lastFailureAnimationTrack:Stop()end end function ClickToMoveDisplay.SetWaypointTexture(S)TrailDotIcon=S TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetWaypointTexture()return TrailDotIcon end function ClickToMoveDisplay.SetWaypointRadius(S)TrailDotSize=Vector2.new(S,S)TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetWaypointRadius()return TrailDotSize.X end function ClickToMoveDisplay.SetEndWaypointTexture(S)EndWaypointIcon=S TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetEndWaypointTexture()return EndWaypointIcon end function ClickToMoveDisplay.SetWaypointsAlwaysOnTop(S)WaypointsAlwaysOnTop=S TrailDotTemplate,EndWaypointTemplate,FailureWaypointTemplate=CreateWaypointTemplates()end function ClickToMoveDisplay.GetWaypointsAlwaysOnTop()return WaypointsAlwaysOnTop end return ClickToMoveDisplay
-- originalEndWaypoint is optional, causes the waypoint to tween from that position.
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
