local function v(v,R)if R then if R.Sit then R.Jump=true end R:MoveTo(v)end local E=ClickToMoveDisplay.CreateEndWaypoint(v)ExistingIndicator=E;(coroutine.wrap(function()R.MoveToFinished:wait()E:Destroy()end))()end local function R(v,R,E,e,U)if FFlagUserClickToMoveFollowPathRefactor then ExistingPather=v end v:Start(U)if not FFlagUserClickToMoveFollowPathRefactor then CleanupPath()end if not FFlagUserClickToMoveFollowPathRefactor then ExistingPather=v end PathCompleteListener=v.Finished.Event:Connect(function()if FFlagUserClickToMoveFollowPathRefactor then CleanupPath()end if E then local v=GetEquippedTool(e)if v then v:Activate()end end end)PathFailedListener=v.PathFailed.Event:Connect(function()CleanupPath()if U==nil or U then local v=PlayFailureAnimation and not(ExistingPather and ExistingPather:IsActive())if v then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(R)end end)end local function E(v)if ExistingPather and ExistingPather:IsActive()then ExistingPather:Cancel()end if PlayFailureAnimation then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(v)end function OnTap(e,U,Z)local a=Workspace.CurrentCamera local F=Player.Character if not CheckAlive()then return end if#e==1 or U then if a then local k=a:ScreenPointToRay(e[1].x,e[1].y)local B=Ray.new(k.Origin,k.Direction*1000)local x=findPlayerHumanoid(Player)local g,J,I=Utility.Raycast(B,true,getIgnoreList())local H,V=Utility.FindCharacterAncestor(g)if Z and(V and StarterGui:GetCore("\065\118\097\116\097\114\067\111\110\116\101\120\116\077\101\110\117\069\110\097\098\108\101\100"))then local v=Players:GetPlayerFromCharacter(V.Parent)if v then CleanupPath()return end end if U then J=U H=nil end if FFlagUserClickToMoveFollowPathRefactor then if J and F then CleanupPath()local v=Pather(J,I)if v:IsValidPath()then R(v,J,H,F)else v:Cleanup()E(J)end end else if UseDirectPath and(J and F)then v(J,x)elseif J and F then local v=Pather(J,I)if v:IsValidPath()then R(v,J,H,F)else if J then E(J)end end end end end elseif#e>=2 then if a then local v=GetEquippedTool(F)if v then v:Activate()end end end end local function e(v)if v then v:Disconnect()end end
-- Remove with FFlagUserClickToMoveFollowPathRefactor
local function HandleDirectMoveTo(hitPt, myHumanoid)
	if myHumanoid then
		if myHumanoid.Sit then
			myHumanoid.Jump = true
		end
		myHumanoid:MoveTo(hitPt)
	end

	local endWaypoint = ClickToMoveDisplay.CreateEndWaypoint(hitPt)
	ExistingIndicator = endWaypoint
	coroutine.wrap(function()
		myHumanoid.MoveToFinished:wait()
		endWaypoint:Destroy()
	end)()
end

local function HandleMoveTo(thisPather, hitPt, hitChar, character, overrideShowPath)
	if FFlagUserClickToMoveFollowPathRefactor then
		ExistingPather = thisPather
	end

	thisPather:Start(overrideShowPath)

	if not FFlagUserClickToMoveFollowPathRefactor then
		CleanupPath()
	end

	if not FFlagUserClickToMoveFollowPathRefactor then
		ExistingPather = thisPather
	end

	PathCompleteListener = thisPather.Finished.Event:Connect(function()
		if FFlagUserClickToMoveFollowPathRefactor then
			CleanupPath()
		end
		if hitChar then
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end)
	PathFailedListener = thisPather.PathFailed.Event:Connect(function()
		CleanupPath()
		if overrideShowPath == nil or overrideShowPath then
			local shouldPlayFailureAnim = PlayFailureAnimation and not (ExistingPather and ExistingPather:IsActive())
			if shouldPlayFailureAnim then
				ClickToMoveDisplay.PlayFailureAnimation()
			end
			ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
		end
	end)
end

local function ShowPathFailedFeedback(hitPt)
	if ExistingPather and ExistingPather:IsActive() then
		ExistingPather:Cancel()
	end
	if PlayFailureAnimation then
		ClickToMoveDisplay.PlayFailureAnimation()
	end
	ClickToMoveDisplay.DisplayFailureWaypoint(hitPt)
end

function OnTap(tapPositions, goToPoint, wasTouchTap)
	-- Good to remember if this is the latest tap event
	local camera = Workspace.CurrentCamera
	local character = Player.Character

	if not CheckAlive() then
		return
	end

	-- This is a path tap position
	if #tapPositions == 1 or goToPoint then
		if camera then
			local unitRay = camera:ScreenPointToRay(tapPositions[1].x, tapPositions[1].y)
			local ray = Ray.new(unitRay.Origin, unitRay.Direction * 1000)

			local myHumanoid = findPlayerHumanoid(Player)
			local hitPart, hitPt, hitNormal = Utility.Raycast(ray, true, getIgnoreList())

			local hitChar, hitHumanoid = Utility.FindCharacterAncestor(hitPart)
			if wasTouchTap and hitHumanoid and StarterGui:GetCore("AvatarContextMenuEnabled") then
				local clickedPlayer = Players:GetPlayerFromCharacter(hitHumanoid.Parent)
				if clickedPlayer then
					CleanupPath()
					return
				end
			end
			if goToPoint then
				hitPt = goToPoint
				hitChar = nil
			end
			if FFlagUserClickToMoveFollowPathRefactor then
				if hitPt and character then
					-- Clean up current path
					CleanupPath()
					local thisPather = Pather(hitPt, hitNormal)
					if thisPather:IsValidPath() then
						HandleMoveTo(thisPather, hitPt, hitChar, character)
					else
						-- Clean up
						thisPather:Cleanup()
						-- Feedback here for when we don't have a good path
						ShowPathFailedFeedback(hitPt)
					end
				end
			else
				if UseDirectPath and hitPt and character then
					HandleDirectMoveTo(hitPt, myHumanoid)
				elseif hitPt and character then
					local thisPather = Pather(hitPt, hitNormal)
					if thisPather:IsValidPath() then
						HandleMoveTo(thisPather, hitPt, hitChar, character)
					else
						if hitPt then
							-- Feedback here for when we don't have a good path
							ShowPathFailedFeedback(hitPt)
						end
					end
				end
			end
		end
	elseif #tapPositions >= 2 then
		if camera then
			-- Do shoot
			local currentWeapon = GetEquippedTool(character)
			if currentWeapon then
				currentWeapon:Activate()
			end
		end
	end
end

local function DisconnectEvent(event)
	if event then
		event:Disconnect()
	end
end
