--global functions

ai.Move = function(loc, mustSee, humanoid)
	goalHumanoid = humanoid
	if goalPos then
		goalPos = loc
	else
		goalPos = loc
		Spawn(function()
			ai.State = "Tracking"
			local lastGoal
			local lastTime = time()
			while goalPos and ai.Humanoid do
				local gp = goalPos
				if pcall(function() return goalPos.X end) then
				elseif goalPos:IsA("BasePart") then
					gp = (ai.canSee(goalPos) or not mustSee) and goalPos.Position or nil
					lastTime = time()
				elseif goalPos:IsA("Model") then
					gp = nil
					for _, bodyPart in pairs(goalPos:GetChildren()) do
						if bodyPart:IsA("BasePart") and (ai.canSee(bodyPart) or not mustSee) then
							gp = goalPos:GetModelCFrame().p
							lastTime = time()
							break
						end
					end
				end
				lastGoal = gp and humanoid and gp + (gp - ai.Model.Torso.Position).unit * 10 or gp or lastGoal or ai.Model.Torso.Position
				if (goalHumanoid and goalHumanoid.Health <= 0) or (not goalHumanoid and (ai.Model.Torso.Position - lastGoal).magnitude < 3) or time() - lastTime > 3 then
					goalPos = nil
				else
					ai.Humanoid.Jump = objAhead(lastGoal)
					ai.Humanoid:MoveTo(lastGoal, workspace.Terrain)
					if goalHumanoid and ((ai.Model.Torso.Position - goalPos:GetModelCFrame().p) * Vector3.new(1,.5,1)).magnitude < 3 and time() - lastAttack > attackTime then
						attack(goalHumanoid)
					end
				end
				wait(.1)
			end
			if ai then
				ai.State = "Idle"
			end
		end)
	end
end



