local function z(z,i)if i then if i.Sit then i.Jump=true end i:MoveTo(z)end local P=ClickToMoveDisplay.CreateEndWaypoint(z)ExistingIndicator=P;(coroutine.wrap(function()i.MoveToFinished:wait()P:Destroy()end))()end local function i(z,i,P,J,L)if FFlagUserClickToMoveFollowPathRefactor then ExistingPather=z end z:Start(L)if not FFlagUserClickToMoveFollowPathRefactor then CleanupPath()end if not FFlagUserClickToMoveFollowPathRefactor then ExistingPather=z end PathCompleteListener=z.Finished.Event:Connect(function()if FFlagUserClickToMoveFollowPathRefactor then CleanupPath()end if P then local z=GetEquippedTool(J)if z then z:Activate()end end end)PathFailedListener=z.PathFailed.Event:Connect(function()CleanupPath()if L==nil or L then local z=PlayFailureAnimation and not(ExistingPather and ExistingPather:IsActive())if z then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(i)end end)end local function P(z)if ExistingPather and ExistingPather:IsActive()then ExistingPather:Cancel()end if PlayFailureAnimation then ClickToMoveDisplay.PlayFailureAnimation()end ClickToMoveDisplay.DisplayFailureWaypoint(z)end function OnTap(J,L,Q)local m=Workspace.CurrentCamera local F=Player.Character if not CheckAlive()then return end if#J==1 or L then if m then local A=m:ScreenPointToRay(J[1].x,J[1].y)local R=Ray.new(A.Origin,A.Direction*1000)local n=findPlayerHumanoid(Player)local u,D,l=Utility.Raycast(R,true,getIgnoreList())local a,d=Utility.FindCharacterAncestor(u)if Q and(d 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 z=Players:GetPlayerFromCharacter(d.Parent)if z then CleanupPath()return end end if L then D=L a=nil end if FFlagUserClickToMoveFollowPathRefactor then if D and F then CleanupPath()local z=Pather(D,l)if z:IsValidPath()then i(z,D,a,F)else z:Cleanup()P(D)end end else if UseDirectPath and(D and F)then z(D,n)elseif D and F then local z=Pather(D,l)if z:IsValidPath()then i(z,D,a,F)else if D then P(D)end end end end end elseif#J>=2 then if m then local z=GetEquippedTool(F)if z then z:Activate()end end end end local function J(z)if z then z:Disconnect()end end

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
